0

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();
      }
  });
rob mayoff
  • 375,296
  • 67
  • 796
  • 848
Andy M
  • 3
  • 1

1 Answers1

0

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

});

Ferran
  • 1,442
  • 1
  • 6
  • 10
  • ok, then write down the code please? because when I tried to add the AnimationDrawable to AnimatorSet, it's says it's incompatible with Animator – Andy M Jun 07 '19 at 21:09