我有两个序列动画(xml 文件)。我想在第一个动画停止时开始第二个动画。这是我的源代码:
mFingerprintIcon.setImageResource(0);
mFingerprintIcon.setBackgroundResource(R.drawable.finger_print_first_animation);
final AnimationDrawable animFirst = (AnimationDrawable) mFingerprintIcon.getBackground();
mFingerprintStatus.setTextColor(ContextCompat.getColor(getActivity(), R.color.success_color));
mFingerprintStatus.setText(getResources().getString(R.string.fingerprint_success));
int iDuration = 0;
for (int i = 0; i < animFirst.getNumberOfFrames(); i++) {
iDuration += animFirst.getDuration(i);
}
animFirst.start();
Handler handler2 = new Handler();
handler2.postDelayed(new Runnable() {
@Override
public void run() {
animFirst.stop();
mFingerprintIcon.setBackgroundResource(R.drawable.finger_print_second_animation);
AnimationDrawable animSecond = (AnimationDrawable) mFingerprintIcon.getBackground();
animSecond.setOneShot(false);
animSecond.start();
}
}, iDuration);
这段代码可以工作,但是有一个问题。第二个动画卡住几秒钟然后开始。
如何编写可以在不卡住的情况下为两个动画设置动画的代码?
最佳答案
使用 AnimationListener :
public class YourClass extends Activity implements Animation.AnimationListener {
...
animFirst.setListener(this);
并在 onAnimationEnd 中开始您的第二个动画方法。
或者在匿名内部类中:
animFirst.setListener(new AnimationListener() {
public void onAnimationEnd(Animation animation) {
animSecond.start();
}
public void onAnimationRepeat(Animation animation) {
}
public void onAnimationStart(Animation animation) {
}
});
关于android - Android 中启动前序列动画卡住,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41037670/