Eddie
Eddie

Reputation: 31

Execute statement after animation is done

What I am trying to do is make the ImageView shake at button press, and when the animation is over, the image from ImageView disappears.

I want this statement to execute first:

ObjectAnimator
                .ofFloat(img, "translationX", 0, 25, -25, 25, -25, 15, -15,     6, -6, 0)
                .setDuration(300)
                .start();

and after the animation is over, this statement should execute:

        img.setImageResource(android.R.color.transparent);

I have them one after another, so the animation doesn't end the pictures are gone.

Upvotes: 0

Views: 499

Answers (2)

Arsal Imam
Arsal Imam

Reputation: 2982

Use the below code,

ObjectAnimator.ofFloat(img, "translationX", 0, 25, -25, 25, -25, 15, -15,     6, -6, 0).addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                Log.e("Animation-Completed:", "onAnimationEnd");
                img.setImageResource(android.R.color.transparent);
            }
        }).setDuration(300).start();

Edit: to add duration:

ObjectAnimator anim = ObjectAnimator.ofFloat(img, "translationX", 0, 25, -25, 25, -25, 15, -15,     6, -6, 0);
anim.setDuration(300);                  // Duration in milliseconds
anim.addListener(new AnimatorListenerAdapter() {
                @Override
                public void onAnimationEnd(Animator animation) {
                    super.onAnimationEnd(animation);
                    Log.e("Animation-Completed:", "onAnimationEnd");
                    img.setImageResource(android.R.color.transparent);
                }
            }); 
anim.start();

Upvotes: 1

Siraj Sumra
Siraj Sumra

Reputation: 954

Use Below code:

ObjectAnimator
            .ofFloat(img, "translationX", 0, 25, -25, 25, -25, 15, -15,     6, -6, 0)
            .setDuration(300)
            .start()
            .addListener(new Animator.AnimatorListener() {
                @Override
                public void onAnimationStart(Animator animator) {

                }

                @Override
                public void onAnimationEnd(Animator animator) {
                   img.setImageResource(android.R.color.transparent);
                }

                @Override
                public void onAnimationCancel(Animator animator) {

                }

                @Override
                public void onAnimationRepeat(Animator animator) {

                }
            });

Upvotes: 0

Related Questions