How to use StateListAnimator?

In Android L a new xml attribute has been added for View :

android:stateListAnimator   : Sets the state-based animator for the View.

Additionally for instantiating StateListAnimator object programmatically a new method :

loadStateListAnimator(Context context, int id)

has been added to AnimatorInflater .

These can be found on Android L developer preview documentation package.


I use this code in java and works fine

if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
            crd.setStateListAnimator(AnimatorInflater.loadStateListAnimator(ctx,
                    R.drawable.card_smooth_shadow));
}

And my animator/card_smooth_shadow.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
    <set>
        <objectAnimator android:propertyName="translationZ"
            android:duration="@android:integer/config_shortAnimTime"
            android:valueTo="10dp"
            android:valueType="floatType"/>
    </set>
</item>
<item
    android:state_pressed="false">
    <set>
        <objectAnimator android:propertyName="translationZ"
            android:duration="100"
            android:valueTo="2dp"
            android:valueType="floatType"/>
    </set>
</item>

RESULT

result