Start two animation in the same Layout

Georgevik picture Georgevik · Jan 19, 2013 · Viewed 9.5k times · Source

Im trying to do an alpha and translate in a RelativeLayout. I define both:

AlphaAnimation alpha;
alpha = new AlphaAnimation(0.0f, 1.0f);
alpha.setDuration(1500);
alpha.setFillAfter(true);

TranslateAnimation translate;
translate = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0,                                                                       
                 Animation.RELATIVE_TO_SELF, 0,
                 Animation.RELATIVE_TO_SELF, 1, 
                 Animation.RELATIVE_TO_SELF, 0);
translate.setDuration(1000);

So I start the animation in my RelativeLayout

RelativeLayout.startAnimation(translate);
RelativeLayout.startAnimation(alpha);

The problem is that in this case, only the alpha animation start and not the translation. Can someone help me? The question is how can I start two different animations at the same time in the same object(Relative Layout in my case)


I resolve the question. I added it:

AnimationSet animationSet = new AnimationSet(true);
animationSet.addAnimation(alpha);
animationSet.addAnimation(translate);

RelativeLayout.startAnimation(animationSet);

Answer

ihsanbal picture ihsanbal · Jul 22, 2014

You can use animation set ıf you want to run two animation in the same time:

http://developer.android.com/reference/android/view/animation/AnimationSet.html

For exeample;

as = new AnimationSet(true);
as.setFillEnabled(true);
as.setInterpolator(new BounceInterpolator());

TranslateAnimation ta = new TranslateAnimation(-300, 100, 0, 0); 
ta.setDuration(2000);
as.addAnimation(ta);

TranslateAnimation ta2 = new TranslateAnimation(100, 0, 0, 0); 
ta2.setDuration(2000);
ta2.setStartOffset(2000); // allowing 2000 milliseconds for ta to finish
as.addAnimation(ta2);