[英]Elevation animation on click on CardView
I want to add the elevation animation to my android.support.v7.widget.CardView
, just like the material style Button
s do. 我想将高程动画添加到我的
android.support.v7.widget.CardView
,就像Button
的材质一样。 I've tried to set a StateListAnimator
: 我试过设置一个
StateListAnimator
:
android:stateListAnimator="@anim/selector_raise"
which points to my selector in res/anim
: 它指向
res/anim
选择器:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="true" android:state_pressed="true">
<objectAnimator android:duration="@android:integer/config_shortAnimTime"
android:propertyName="translationZ" android:valueTo="@dimen/touch_raise"
android:valueType="floatType" />
</item>
<item>
<objectAnimator android:duration="@android:integer/config_shortAnimTime"
android:propertyName="translationZ" android:valueTo="0dp"
android:valueType="floatType" />
</item>
</selector>
but Android Studio gives me the error: 但是Android Studio给了我错误:
Element selector must be declared
必须声明元素选择器
What's the right way to do that? 这样做的正确方法是什么?
I've tried your code, maybe you have simply add the state to second selector element. 我已经尝试过你的代码,也许你只需将状态添加到第二个选择器元素。
So change this line 所以改变这一行
<item>
with this 有了这个
<item android:state_enabled="true" android:state_pressed="false">
The complete code will be 完整的代码将是
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="true" android:state_pressed="true">
<objectAnimator android:duration="@android:integer/config_shortAnimTime"
android:propertyName="translationZ" android:valueTo="@dimen/touch_raise"
android:valueType="floatType" />
</item>
<item android:state_enabled="true" android:state_pressed="false">
<objectAnimator android:duration="@android:integer/config_shortAnimTime"
android:propertyName="translationZ" android:valueTo="0dp"
android:valueType="floatType" />
</item>
</selector>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.