How to combine an AnimationDrawable with another animation?

267 Views Asked by At

Is it possible to combine these 2 animations below? It works but it seems weird to have to call them separately ( 2 calls to start() )

    ImageView rocketImage = (ImageView) findViewById(R.id.rocket_image);
    rocketImage.setBackgroundResource(R.drawable.rocket_thrust);
    rocketAnimation = (AnimationDrawable) rocketImage.getBackground();

    rocketImage.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View view) {
        rocketAnimation.start();
        rocketImage.animate().x(100).y(100).withLayer().setDuration(500).start();
      }
  });
1

There are 1 best solutions below

1
Ferran On BEST ANSWER

See AnimatorSet and use the function playTogether. However, as you mentioned in your comments, AnimationDrawable is not compatible with AnimatorSet. In this case, to ensure both animations start together, you can write it like this

AnimationDrawable rocketAnimation = (AnimationDrawable) rocketImage.getBackground();

rocketImage.setOnClickListener(new View.OnClickListener() {
  @Override
  public void onClick(View view) {
    rocketImage.animate().x(100).y(100).withLayer().setDuration(500).setListener(new Animator.AnimatorListener() {
        @Override
        public void onAnimationStart(Animator animation) {
            rocketAnimation.start();
        }

        @Override
        public void onAnimationEnd(Animator animation) {

        }

        @Override
        public void onAnimationCancel(Animator animation) {

        }

        @Override
        public void onAnimationRepeat(Animator animation) {

        }
    }).start();
  }

});