17

I want to run the three translate animations shown below sequentially. i.e. after one translate animation ends, the second translate animation starts. However, they run concurrently.

Additionally, this animation will be used for overridePendingTransition() as a parameter. So, I have to solve this problem, only by using XML code.

Is there anyone who knows what I should do?

<set
xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/linear_interpolator"
android:fillAfter="true">

<translate
    android:fromXDelta="100%p"
    android:toXDelta="-20%p"
    android:duration="1000" />

<translate
    android:fromXDelta="-20%p"
    android:toXDelta="20%p"
    android:duration="1000" />

<translate
    android:fromXDelta="20%p"
    android:toXDelta="0"
    android:duration="1000" />
</set>
darrenp
  • 4,265
  • 2
  • 26
  • 22
howisgeek
  • 285
  • 2
  • 3
  • 9

2 Answers2

36

Use android:startOffset to delay animations.

With your example, this should do what you want:

<set
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/linear_interpolator"
    android:fillAfter="true">

    <translate
        android:fromXDelta="100%p"
        android:toXDelta="-20%p"
        android:duration="1000" />

    <translate
        android:startOffset="1000"
        android:fromXDelta="-20%p"
        android:toXDelta="20%p"
        android:duration="1000" />

    <translate
        android:startOffset="2000"
        android:fromXDelta="20%p"
        android:toXDelta="0"
        android:duration="1000" />
</set>
Kevin Gaudin
  • 9,927
  • 3
  • 32
  • 34
  • Clever! Using startOffset you time when each translate happens. Very nice. – portfoliobuilder Aug 06 '15 at 19:16
  • What if there are multiple views, say 10 or say unknown amount of views. In that case how can someone rely on set tag. Assuming we wont stupidly create 10 translate item. – Alex Dec 19 '16 at 04:28
  • This will not work if the animation is supposed to loop. In that case, you can use the method described below, by implementing animation listeners. – NobodySomewhere Jul 02 '20 at 09:18
18

Kevin's solution may work, though I have found the timing to be somewhat inaccurate.

Another option is to use a pair of AnimationListener objects, one attached to each of the first two Animations. The listener for the first animation, in onAnimationEnd(), would start the second animation. The listener for the second animation, in onAnimationEnd(), would start the third animation.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • 3
    I didn't include this in my answer because howisgeek explained that he needs a full xml solution. Otherwise these callbacks can be useful even if I experienced timing issues with those too (onAnimatonEnd() triggered a few milliseconds before the real animation end). – Kevin Gaudin Dec 19 '10 at 12:08
  • 1
    @Kevin Gaudin: Oops, yeah, I missed that. However, frankly, that sort of restriction feels like a student asking us to do the homework assignment -- I can think of no reason why you would restrict the solution to XML-only in a real app. – CommonsWare Dec 19 '10 at 12:11
  • 2
    I like the student homework comparison ;-) But Activity.overridePendingTransition(enterAnim, exitAnim) actually takes only resource Ids as input parameters. There might be other ways of providing these animations by subclassing Activity but that would be overly complicated. – Kevin Gaudin Dec 19 '10 at 12:26
  • 4
    @Kevin Gaudin: ::smacks forehead:: Very good point. I forget sometimes that there are scenarios where you can't use Java with animations. Of course, what would be slick is for sequential animation transitions like this to be encodeable in XML, using an `android:id` and `android:onComplete` attribute pair or something. – CommonsWare Dec 19 '10 at 12:53
  • 3
    My animation "shakes" my acitivity if I do it sequentially for some reason: http://stackoverflow.com/questions/18899280/multiple-xml-animation-is-not-working – Si8 Sep 19 '13 at 16:51
  • @SiKni8 if your animation shakes or blinks, you need to clear animation in onAnimationEnd of your first animation before starting the second. It has been experienced that two animations assigned to one object at the same time results into some inconsistencies. – Masoud Dadashi Dec 19 '15 at 01:04
  • Also, this answer assumes that the end callback is delivered actually after the animation has ended. Unfortunately, due to bug or whatever, it doesn't work like that. https://stackoverflow.com/q/4750939/3175580 – androidguy Aug 16 '17 at 23:39