Animate selector/state transitions

C.d. picture C.d. · Feb 10, 2012 · Viewed 21.2k times · Source

I have a simple selector for my ListView

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

    <item android:drawable="@drawable/yellow_arc" android:state_activated="true"/>
    <item android:drawable="@drawable/yellow_nonarc" android:state_activated="false"/>

</selector>

I want to animate the transition between these drawables when the state of the views are changed from activated to not-activated and vica versa.

If you run the example in API demos you will see an obvious fade-in/fade-out animation while the activated state of the view is changed.

So what I want is a custom animation while the state of the view is changed. I think it should be done via xml but I couldn't find a way.

Thanks in advance.

EDIT:

I guess I have found something useful there's a activated_background.xml in \Android\android-sdk\platforms\android-API_VERSION\data\res\drawable which includes

<selector xmlns:android="http://schemas.android.com/apk/res/android"
        android:exitFadeDuration="@android:integer/config_mediumAnimTime">
    <item android:state_activated="true" android:drawable="@android:drawable/list_selector_background_selected" />
    <item android:drawable="@color/transparent" />
</selector>

So the example in API-demos achieveing this fade-out animation by declaring an exitFadeDuration. However, this is not exactly what I want.. I want to declare custom animations for the transition between the state drawables since the fade-in/fade-out animation does not look good for my drawables.

Answer

Larry McKenzie picture Larry McKenzie · Nov 6, 2015

Added in api 21 "StateListAnimator"

http://developer.android.com/reference/android/animation/StateListAnimator.html

I know this is an old question but this may help future people looking to do this.