Android随手记 BUTTON实现点击下沉效果

先看下效果:


BUTTON1.gif

通过增强/减弱 CaradView的Elevation(阴影)范围来给视觉造成BUTTON下沉的错觉效果,因为是自带的API,所以设置也很简单。

首先创建资源文件button_sink.xml至/res/animator
代码部分:

<?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="0dp"
            android:valueType="floatType" />
    </item>
    <item>
        <objectAnimator
            android:duration="@android:integer/config_shortAnimTime"
            android:propertyName="translationZ"
            android:valueTo="5dp"
            android:valueType="floatType" />
    </item>
</selector>

在资源文件中创建layout-v21 , 并将需要使用的布局重新Copy一份至此(Android 5.0+才可使用)。
再layout-v21的布局的CardView中加入android:stateListAnimator="@animator/button_sink"即可食用。

如需点击波纹效果,加入android:foreground="?attr/selectableItemBackground"即可

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容