How to use StateListAnimator?
Asked Answered
W

3

27

From the docs:

The new StateListAnimator class lets you define animators that run when the state of a view changes. The following example shows how to define an StateListAnimator as an XML resource:

<!-- animate the translationZ property of a view when pressed --> <selector xmlns:android="http://schemas.android.com/apk/res/android">  
  <item android:state_pressed="true">
    <set>
      <objectAnimator android:propertyName="translationZ"
        android:duration="100"
        android:valueTo="2"
        android:valueType="floatType"/>
        <!-- you could have other objectAnimator elements
             here for "x" and "y", or other properties -->
    </set>   
  </item>   
  <item android:state_enabled="true"
    android:state_pressed="false"
    android:state_focused="true">
    <set>
      <objectAnimator android:propertyName="translationZ"
        android:duration="100"
        android:valueTo="2"
        android:valueType="floatType"/>
    </set> 
  </item> 
</selector>

However, it says nothing about how to actually use this xml file. There seems to be no method on the Resources class to get a StateListAnimator, and the StateListAnimator class does not provide any info either.

How can we use this?

Wacke answered 27/6, 2014 at 18:27 Comment(0)
A
27

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.

Argot answered 27/6, 2014 at 19:3 Comment(2)
For a code snippet, try StateListAnimator sla = AnimatorInflater.loadStateListAnimator(context, R.anim.my_anim); View.setStateListAnimator(sla);Phosphorism
Say, why does AppBarLayout have a default stateListAnimator ? When does it ever get changed? Which states does it have, by default?Dollop
D
15

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

Dispenser answered 26/9, 2018 at 12:4 Comment(4)
How do you have a ripple effect with animator?Mahogany
@Dr. aNdRO It's not the StateListAnimator but to apply that effect to any view, in the layout use android:background="?selectableItemBackground"Mckenney
@TimKist that work's too, but main usage of StateListAnimator here is shadow effect . as you can see when we click (or hold) cardView the shadow increasesDispenser
Yeah, thanks @Radesh. I was referring to the ripple effect Dr. aNdRO was asking about, not your answer. That was a good explanationMckenney
L
0

Lets you define a number of Animators that will run on the attached View depending on the View's drawable state.

It can be defined in an XML file with the element. Each State Animator is defined in a nested element.

The new StateListAnimator class lets you define animators that run when the state of a view changes. The following example shows how to define an StateListAnimator as an XML resource: https://indiacodinghub.blogspot.com/2021/01/how-to-use-statelistanimator.html

Lusaka answered 1/2, 2021 at 7:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.