android - AnimationSet 中的 TranslateAnimation 不从 0 位置开始,为什么?

标签 android animation

我希望按钮向下移动 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/

相关文章:

wpf - 在 WPF 中滑动面板的最佳方法是什么?

javascript - 如何循环播放来自同一 src 的动画和淡入淡出图像?

java - Android动画淡入/淡出未在回调函数中设置 View 的可见性

android - 唤醒 Google Glass 屏幕以根据位置显示当前信息

Android:使用屏幕上的软键盘仅调整部分 View 的大小

几次 setAnimation 调用后 Android View 旋转没有发生

ios - 侧面菜单的框翻转 3D 动画

android - 失败 [INSTALL_FAILED_OLDER_SDK] Android Studio

android - 旋转手机在 Android 上重新启动视频

android - 多个dex文件定义Lorg/apache/commons/logging/impl/LogFactoryImpl