AnimationSet 未按预期执行顺序动画

问题描述 投票:0回答:1

如果我手动执行多个连续动画。它按预期工作。这是我的可行代码

scale_up.xml

<?xml version="1.0" encoding="utf-8"?>
<scale xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromXScale="1.0"
    android:fromYScale="1.0"
    android:toXScale="1.1"
    android:toYScale="1.1"
    android:pivotX="50%"
    android:pivotY="50%"
    android:fillAfter="true"
    android:interpolator="@android:anim/decelerate_interpolator"
    android:duration="@android:integer/config_shortAnimTime" />

scale_down.xml

<?xml version="1.0" encoding="utf-8"?>
<scale xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromXScale="1.1"
    android:fromYScale="1.1"
    android:toXScale="1.0"
    android:toYScale="1.0"
    android:pivotX="50%"
    android:pivotY="50%"
    android:fillAfter="true"
    android:interpolator="@android:anim/decelerate_interpolator"
    android:duration="@android:integer/config_shortAnimTime" />

手动执行连续动画

public void startAnimation(Button button) {
    // Define the scale up animation
    Animation scaleUpAnimation = AnimationUtils.loadAnimation(this, R.anim.scale_up);

    // Define the scale down animation
    Animation scaleDownAnimation = AnimationUtils.loadAnimation(this, R.anim.scale_down);

    scaleUpAnimation.setAnimationListener(new Animation.AnimationListener() {
        @Override
        public void onAnimationStart(Animation animation) {

        }

        @Override
        public void onAnimationEnd(Animation animation) {
            button.startAnimation(scaleDownAnimation);
        }

        @Override
        public void onAnimationRepeat(Animation animation) {

        }
    });

    button.startAnimation(scaleUpAnimation);
}

结果


但是,如果我尝试使用

AnimationSet
替换上述代码,动画结果就会损坏。

public void startAnimation(Button button) {
    // Define the scale up animation
    Animation scaleUpAnimation = AnimationUtils.loadAnimation(this, R.anim.scale_up);

    // Define the scale down animation
    Animation scaleDownAnimation = AnimationUtils.loadAnimation(this, R.anim.scale_down);

    // Create an AnimationSet to combine both animations
    // (It makes no difference whether I am using true or false)
    AnimationSet animationSet = new AnimationSet(true);
    animationSet.addAnimation(scaleUpAnimation);
    animationSet.addAnimation(scaleDownAnimation);

    // Apply the animation to the button
    button.startAnimation(animationSet);
}

使用AnimationSet的结果(动画不流畅)

我可以知道为什么

AnimationSet
不起作用吗?谢谢。

java android android-animation
1个回答
0
投票

我们需要使用

setStartOffset
来延迟第二个动画的执行。这是解决上述问题的完整代码片段。

public void startAnimation(Button button) {
    int config_shortAnimTime = getResources().getInteger(android.R.integer.config_shortAnimTime);

    // Define the scale up animation
    ScaleAnimation scaleUpAnimation = new ScaleAnimation(
            1f, 1.02f, 1f, 1.02f,
            Animation.RELATIVE_TO_SELF, 0.5f,
            Animation.RELATIVE_TO_SELF, 0.5f
    );
    scaleUpAnimation.setInterpolator(new DecelerateInterpolator());
    scaleUpAnimation.setDuration(config_shortAnimTime);
    scaleUpAnimation.setFillAfter(true);

    // Define the scale down animation
    ScaleAnimation scaleDownAnimation = new ScaleAnimation(
            1.02f, 1f, 1.02f, 1f,
            Animation.RELATIVE_TO_SELF, 0.5f,
            Animation.RELATIVE_TO_SELF, 0.5f
    );
    scaleDownAnimation.setInterpolator(new AccelerateInterpolator());
    scaleDownAnimation.setDuration(config_shortAnimTime);
    scaleDownAnimation.setFillAfter(true);

    scaleDownAnimation.setStartOffset(scaleUpAnimation.getDuration());

    // Create an AnimationSet to combine both animations
    AnimationSet animationSet = new AnimationSet(false);
    animationSet.addAnimation(scaleUpAnimation);
    animationSet.addAnimation(scaleDownAnimation);

    // Apply the animation to the button
    button.startAnimation(animationSet);
}
© www.soinside.com 2019 - 2024. All rights reserved.