I have two TranslateAnimations on a TextView and I want them to execute one after other. However, by using the code below, only the second one is executed.
How can I solve this?
TranslateAnimation animation = new TranslateAnimation(
Animation.ABSOLUTE, 0.0f, Animation.ABSOLUTE, 0.0f,
Animation.ABSOLUTE, 0.0f, Animation.ABSOLUTE, -150.0f);
animation.setDuration(200);
wave.startAnimation(animation);
TranslateAnimation animation1 = new TranslateAnimation(
Animation.ABSOLUTE, 0.0f, Animation.ABSOLUTE, 0.0f,
Animation.ABSOLUTE, 150.0f, Animation.ABSOLUTE, 0.0f);
animation1.setDuration(200);
wave.startAnimation(animation1);
If you use code, you can call
to delay the second animation.
if you use xml you can
android:ordering="sequentially"
property to make the two animations perform sequentially.Create an animation array and use method for creating AnimationSet.
Method:
also you can do this by XML itself using
android:startOffset
attribute , and there is an examble:Link them together with Animation Set
EDIT: Andy Boots answer below is the better answer imo.
Just set your first one like this and it'll start the other one, once the animation finishes:
edit: The reason only your second animation is executed with your current code, is because it overrides the playing of the first animation (both actually are played, but you only see the latest one to start). If you do like I wrote, they will play sequentially instead of in parallel.
There is one more approach to reach this goal which can be useful when you need to animate a lot of views one after another. You can use
setStartOffset
method to set a delay before animation begins. So, if you know, how much time will take for your first animation to end, you can set this as a delay for your second animation. This is an example where I animated sixImageButtons
and sixTextViews
below them one after another:In my
res/anim
folder I have a file, calledfade.xml
with these contents: