简体   繁体   English

单击CardView上的高程动画

[英]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>

You tried to create this .xml in res/anim folder. 您试图在res/anim文件夹中创建此.xml。

You should create on res/animator , if don't exist is easy to create. 您应该在res/animator上创建,如果不存在则很容易创建。

But if you search for the problem it already give you a possible solution 但如果您搜索问题,它已经为您提供了可能的解决方案 在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM