user3622171 user3622171 - 6 months ago 29
Android Question

android scale animation suddenly goes back to original size

I have this animation where it scales up a little bit, then scales back down to the original size. But as its scaling back down to its original size, it slowly scales down, the its suddenly at its original size. And no, its not the accelerate decelerate interpolator, i also tried the linear one and it did the same.

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:startOffset="1000"
android:shareInterpolator="true">
<alpha
android:fromAlpha="0.0"
android:toAlpha="1.0"
android:duration="1000" />
<scale
android:duration="1000"
android:fromXScale="1.0"
android:fromYScale="1.0"
android:pivotX="25%p"
android:pivotY="75%p"
android:toXScale="1.1"
android:toYScale="1.1" />
<set
android:startOffset="500">
<scale
android:duration="1000"
android:fromXScale="1.1"
android:fromYScale="1.1"
android:pivotX="25%p"
android:pivotY="75%p"
android:toXScale="1.0"
android:toYScale="1.0" />
</set>
</set>

Answer

Your first scale animations duration is 1000. The second one starts after 500ms. So they are interfering. Maybe that's your problem. You could try to set the second animations start offset to 1000. If this doesn't help you could take a look at Animation.fillAfter()

EDIT: This works

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="true"
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:shareInterpolator="true"
android:startOffset="1000">
<alpha
    android:fromAlpha="0.0"
    android:toAlpha="1.0"
    android:duration="1000" />
<scale
    android:duration="1000"
    android:fromXScale="1.0"
    android:fromYScale="1.0"
    android:pivotX="25%p"
    android:pivotY="75%p"
    android:toXScale="1.1"
    android:toYScale="1.1" />
<scale
    android:duration="1000"
    android:fromXScale="1.0"
    android:fromYScale="1.0"
    android:pivotX="25%p"
    android:pivotY="75%p"
    android:startOffset="1000"
    android:toXScale="0.9"
    android:toYScale="0.9" />
</set>

And you don't have to have a second set inside the set, just add the startOffset directly to the scale animation.

And I just realised, that the second scale has to use 1.0 and 0.9 instead of 1.1 and 1.0. Because it takes the percentage of the current size, not the original size.

Comments