我希望按钮向下移动 200px(从 0,0 到 0, 200) 1秒后,再次向上移动到原始位置。
但是以下代码的行为就像从 (0,200) 到 (0,400) 开始,而不是从 (0,0) 开始
如果我将一个动画放入 AnimationSet 中,它就可以正常工作。但如果我在 AnimationSet 中放入两个以上的动画,效果会很奇怪。
有什么问题吗?
public class MainActivity extends Activity {
private static final int DISTANCE = 200;
private static final long DURATION = 1000;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn = (Button) findViewById(R.id.button1);
TranslateAnimation aniMove = new TranslateAnimation(0, 0, 0, DISTANCE);
aniMove.setDuration(DURATION);
TranslateAnimation aniMoveBack = new TranslateAnimation(0, 0, DISTANCE, 0);
aniMoveBack.setDuration(DURATION);
aniMoveBack.setStartOffset(DURATION + 1000);
AnimationSet aniSet = new AnimationSet(true);
aniSet.addAnimation(aniMove);
aniSet.addAnimation(aniMoveBack);
btn.startAnimation(aniSet);
}
}
这是 XML
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="0dp" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
最佳答案
AnimationSet represents a group of Animations that should be played together. The transformation of each individual animation are composed together into a single transform.
AnimationSet不能这样使用,它可以结合多种动画的特性。使用AnimationSet,您可以同时执行alpha 和旋转动画。
如果你想按顺序播放2个动画,你应该在第一个动画的onAnimationEnd()中开始第二个动画。
关于android - AnimationSet 中的 TranslateAnimation 不从 0 位置开始,为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14192728/