在Android中开始之前冻结序列动画

时间:2016-12-08 10:53:01

标签: android android-animation android-imageview

我有两个序列动画(xml文件)。我想在第一个动画停止时开始第二个动画。这是我的源代码:

.post img {
    clear: right;
    float: right;
    width: 150px;
    padding-bottom: 3px;
}

此代码正常运行,但有一个问题。第二个动画冻结了几秒钟,然后开始。

如何编写能够在不冻结动画的情况下制作动画的代码?

2 个答案:

答案 0 :(得分:2)

使用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) {
    }
});

答案 1 :(得分:0)

正确答案。我测试了自己 使用序列动画的最佳方法是以编程方式创建动画。例如像

animSecond = new AnimationDrawable();
    animFirst = new AnimationDrawable();

    animFirst.setOneShot(true);
    animFirst.addFrame(
            getResources().getDrawable(R.drawable.fingerprint_00001),
            50);
    animFirst.addFrame(
            getResources().getDrawable(R.drawable.fingerprint_00002),
            50);



        int iDuration = 0;

    for (int i = 0; i < animFirst.getNumberOfFrames(); i++) {
        iDuration += animFirst.getDuration(i);
    }
    mFingerprintIcon.setImageDrawable(animFirst);
    animFirst.start();


    Handler handler2 = new Handler();
    handler2.postDelayed(new Runnable() {
        @Override
        public void run() {
            animFirst.stop();
            if (animFadeOut != null) {
                mFingerprintStatus.startAnimation(animFadeOut);
                mFingerprintStatus.setVisibility(View.INVISIBLE);
            }
            mFingerprintIcon.setImageDrawable(animSecond);

            animSecond.start();




        }
    }, iDuration);