Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple animations on 1 imageview android

I have 2 animations which are already working, i want to fade my train + tween my train on the same time. If I execute 1 of these lines it works. But if I try to execute both it, only 1 will work.. I really can't find a solution here.

Maybe you can help?

  final ImageView mytrain = (ImageView) findViewById(R.id.train);
  final Animation traintween = AnimationUtils.loadAnimation(this,R.anim.treinanimation);
   final Animation trainfade = AnimationUtils.loadAnimation(this,R.anim.trainfade);


  mytrain.startAnimation(trainfade);
 mytrain.startAnimation(trainntween);

I want mytrain to execute both animations..

Thank you for the help!

like image 706
RobinHo Avatar asked Mar 21 '13 12:03

RobinHo


3 Answers

Use the AnimationSet class:

AnimationSet s = new AnimationSet(false);//false means don't share interpolators
s.addAnimation(traintween);
s.addAnimation(trainfad);
mytrain.startAnimation(s);
like image 157
Korniltsev Anatoly Avatar answered Nov 04 '22 03:11

Korniltsev Anatoly


You need to use an AnimationSet, check out the docs. Just call addAnimation() for each Animation you want to play.

like image 21
dmon Avatar answered Nov 04 '22 03:11

dmon


Can be done programatically using AnimatorSet class of android :

final AnimatorSet mAnimatorSet = new AnimatorSet();
    mAnimatorSet.playTogether(
                ObjectAnimator.ofFloat(img_view,"scaleX",1,0.9f,0.9f,1.1f,1.1f,1),
                ObjectAnimator.ofFloat(img_view,"scaleY",1,0.9f,0.9f,1.1f,1.1f,1),
                ObjectAnimator.ofFloat(img_view,"rotation",0 ,-3 , -3, 3, -3, 3, -3,3,-3,3,-3,0)
        );

//define any animation you want,like above

mAnimatorSet.setDuration(2000); //set duration for animations
    mAnimatorSet.start();

this example will start all the 3 animation on the target view(imgView) at same time ,you can also use playSequentially .....

For complete example check this out..

like image 4
Gagan Avatar answered Nov 04 '22 02:11

Gagan