5

我有一个位于底部上方 250dp 的 ImageView,并且我想将其移动到底部上方 50dp 的平移动画。

我知道如何使用翻译动画,但我不知道 ToYValue 字段是什么。

代码将是这样的:

TranslateAnimation translate = new TranslateAnimation(Animation.RELATIVE_TO_PARENT,0,Animation.RELATIVE_TO_PARENT,0,Animation.ABSOLUTE,250,Animation.ABSOLUTE,50);
translate.setDuration(1000);
translate.reset();  
translate.setFillAfter(true);
iv.clearAnimation();
iv.startAnimation(translate);
4

1 回答 1

2

您需要 fromYValue 为 0,表示从当前位置开始,toYValue 为 50,表示向下移动 50 个像素。请注意,这些值以像素为单位,而不是 dp。如果它必须在 dp 中,那是另一个问题。

关键在于 TranslateAnimation 文档“更改 Y 坐标以在动画开始时应用”中的“更改”一词。

http://developer.android.com/reference/android/view/animation/TranslateAnimation.html

于 2012-03-29T19:15:15.203 回答