BekaKK
BekaKK

Reputation: 2253

Sequence animation freezing before start in Android

I have two sequence animations (xml files). I want to start the second animation when the first animation has stopped. This is my source code :

    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);

This code is working, but has one problem. The second animation is freezing for some seconds and then starting.

How can I write code that can animate both animations without freezing?

Upvotes: 2

Views: 706

Answers (2)

BekaKK
BekaKK

Reputation: 2253

Correct Answer.I tested myself Best way to use Sequence animation is to create animation programmatically.for example like this

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);

Upvotes: 0

A Honey Bustard
A Honey Bustard

Reputation: 3503

Use an AnimationListener :

public class YourClass extends Activity implements Animation.AnimationListener {

    ...
    animFirst.setListener(this);

and start your second animation in the onAnimationEnd method.

Or in an anonymous inner class :

animFirst.setListener(new AnimationListener() {

    public void onAnimationEnd(Animation animation) {
        animSecond.start();
    }

    public void onAnimationRepeat(Animation animation) {
    }

    public void onAnimationStart(Animation animation) {
    }
});

Upvotes: 2

Related Questions