Android 如何使用 StateListAnimator?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24458615/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 08:11:39  来源:igfitidea点击:

How to use StateListAnimator?

androidandroid-5.0-lollipop

提问by nhaarman

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:

新的 StateListAnimator 类允许您定义在视图状态更改时运行的动画器。以下示例显示了如何将 StateListAnimator 定义为 XML 资源:

<!-- 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 usethis xml file. There seems to be no method on the Resourcesclass to get a StateListAnimator, and the StateListAnimatorclass does not provide any info either.

但是,它没有说明如何实际使用这个 xml 文件。Resources类上似乎没有获取 a 的方法StateListAnimator,并且StateListAnimator该类也不提供任何信息。

How can we use this?

我们如何使用它?

回答by harism

In Android La new xml attribute has been added for View:

Android L 中,View添加了一个新的 xml 属性:

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

Additionally for instantiating StateListAnimatorobject programmatically a new method :

此外,为了以编程方式实例化StateListAnimator对象,还有一个新方法:

loadStateListAnimator(Context context, int id)

has been added to AnimatorInflater.

已添加到AnimatorInflater

These can be found on Android Ldeveloper preview documentation package.

这些可以在Android L开发者预览文档包中找到。

回答by Radesh

I use this code in java and works fine

我在java中使用此代码并且工作正常

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

还有我的动画师/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

结果