Android Alpha Animation: Alpha value jumps back to old value after Animation ended
Asked Answered
L

3

11

I have a ImageButton View on a Layout. If the users clicks the ImageButton the button should fade out.

The buttons fadeout animation will be started like this:

public void buttonClicked(View aButton){

    final Animation aAnim = new AlphaAnimation(1.0f, 0.0f);
    aAnim.setDuration(500);

    aButton.startAnimation(aAnim);

}

This works, but as soon as the ImageButton is faded out, its Alpha Value jumps right back to 1.0 and the button is visible again.

I solved it temporarily with a Animation Listener that sets the ButtonImage to invisible at the end of the Animation but that seems to be a odd solution to me.

What do I have to do to keep the buttons Alpha Value at its last value of the Animation?

Thank You.

Lovejoy answered 25/5, 2012 at 14:27 Comment(1)
Your "odd solution" is the way to go. It's a known problem with android animations before honeycomb. The easier method is the one by MikeT but it doesn't always work the way you'd expect it toClearance
E
29

try using aAnim.setFillAfter(true);

Eosinophil answered 25/5, 2012 at 14:31 Comment(0)
Y
3

If you want to set new value when animation finished, you must set the 'setFillAfter' to true.

public void buttonClicked(View aButton){    
    final Animation aAnim = new AlphaAnimation(1.0f, 0.0f);
    aAnim.setDuration(500);

    aAnim.setFillAfter(true);
    aButton.startAnimation(aAnim);
}
Yam answered 2/12, 2012 at 7:9 Comment(1)
aAnim.setFillAfter(true); <- The animation gets the modifier, not the button itself.Vala
C
0

you should also check this on your XML, for some scenarios it happens

android:animateLayoutChanges=“true”

Cassaba answered 13/5, 2019 at 14:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.