android - Android 中启动前序列动画卡住

标签 android android-animation android-imageview

我有两个序列动画(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/

相关文章:

android - 依赖冲突 'com.android.support:support-annotations'

android - 我无法使用计时器播放动画

Android用Path画圆

Javamail : Deleting mail whose class is held as a file

java - GWT 与Android 通信。枚举序列化问题

android - 当应用程序被杀死时,Flutter android不会收到通知

android - 如何在安卓中播放 GIF

android - findViewById 返回错误的元素?

android - 如何在 Android 应用程序中对 imageView 进行分层?

android - 图像文件从本地存储到 ImageView - android 10 存储权限问题