superpuccio
superpuccio

Reputation: 13012

Composing Simple Animations

I need your help to understand how to manage a complex animation built from some simple animations in Android: I have this animation xml file:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">

    <!-- A: executed only one time at the beginning -->
    <scale xmlns:android="http://schemas.android.com/apk/res/android"
        android:duration="3000"
        android:fromXScale="1"
        android:fromYScale="1"
        android:pivotX="50%"
        android:pivotY="50%"
        android:toXScale="0.2"
        android:toYScale="0.2" />

    <!-- B: executed only one time AFTER A -->
    <scale xmlns:android="http://schemas.android.com/apk/res/android"
        android:duration="300"
        android:fromXScale="0.2"
        android:fromYScale="0.2"
        android:pivotX="50%"
        android:pivotY="50%"
        android:toXScale="1"
        android:toYScale="1" />

    <!-- C: Executed 5 times after B -->
    <scale xmlns:android="http://schemas.android.com/apk/res/android"
        android:duration="300"
        android:fromXScale="1"
        android:fromYScale="1"
        android:pivotX="50%"
        android:pivotY="50%"
        android:repeatCount="5"
        android:repeatMode="reverse"
        android:toXScale="0.8"
        android:toYScale="0.8" />

    <!-- AT THE END REPEAT ALL FROM THE A TO C -->
</set>

I put some simple scale animation within the file and, as you can read from the comments, I'd like to create this (infinite) sequence:

A-B-C-C-C-C-C-A-B-C-C-C-C-C-A-B-C-C-C-C-C and so on...

How can I reach my objective? My problems are two: first of all the scale animations order is not follow (it seems that all the animations start together). And then, How can I repeat all the sequence forever? Thank you.

Upvotes: 0

Views: 213

Answers (2)

Snake
Snake

Reputation: 14678

I was running into a similar issue. The best thing to do is to do things in code not in XML. So you set an animation listener for every animation and on AnimationEnd you control whether you want to repeat or fire off the next animation

Upvotes: 1

user4650678
user4650678

Reputation: 1

You can use "startOffset" to start animations delayed:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">

    <!-- A: executed only one time at the beginning -->
    <scale xmlns:android="http://schemas.android.com/apk/res/android"
        android:duration="3000"
        android:fromXScale="1"
        android:fromYScale="1"
        android:pivotX="50%"
        android:pivotY="50%"
        android:toXScale="0.2"
        android:toYScale="0.2" />

    <!-- B: executed only one time AFTER A -->
    <scale xmlns:android="http://schemas.android.com/apk/res/android"
        android:startOffset="3000"
        android:duration="300"
        android:fromXScale="0.2"
        android:fromYScale="0.2"
        android:pivotX="50%"
        android:pivotY="50%"
        android:toXScale="1"
        android:toYScale="1" />

    <!-- C: Executed 5 times after B -->
    <scale xmlns:android="http://schemas.android.com/apk/res/android"
        android:startOffset="3300"
        android:duration="300"
        android:fromXScale="1"
        android:fromYScale="1"
        android:pivotX="50%"
        android:pivotY="50%"
        android:repeatCount="5"
        android:repeatMode="reverse"
        android:toXScale="0.8"
        android:toYScale="0.8" />

    <!-- AT THE END REPEAT ALL FROM THE A TO C -->
</set>

Since you cannot set a repeatCount on a animation set you will have to repeat the animation programmatically.

Upvotes: 0

Related Questions