如果我手动执行多个连续动画。它按预期工作。这是我的可行代码
<?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" />
<?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
不起作用吗?谢谢。
我们需要使用
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);
}