Android Marterial UI 之水波纹效果

作者 : 开心源码 本文共2419个字,预计阅读时间需要7分钟 发布时间: 2022-05-12 共184人阅读

1 相关Android SDK资源

  • 有边界波纹,默认资源:"?android:attr/selectableItemBackground"
  • 无边界波纹,默认资源:"?android:attr/selectableItemBackgroundBorderless"

?android:attr/selectableItemBackground表示引用主题中的selectableItemBackground这个属性,而主题中selectableItemBackground属性指向的是一个drawable资源,当然这个主题属性是兼容了Android平台的,有的属性则没有。

2 普通可点击View设置点击水波纹效果

2.1 使用默认主题的

待补充

2.2 自己设置水波纹

待补充

3 CardView 设置点击水波纹效果

参考CardView 设置点击水波纹效果

设置CardView的android:background属性是不起作用的

  • 可以增加app:cardBackgroundColor设置背景色。
  • 假如需要增加水波纹效果,那么只能增加前景了,那么前景可以使用主题的,也可以使用自己设置的,本质就是个drawable,不过前景大多数情况下是设置点击效果的,不然设置为纯色或者图片,就会把子View给遮住了,除非你不要显示子View
  • 假如需要给CarView加非颜色背景,那加一个FrameLayout子View吧,把背景图设置在FrameLayout的背景里

3.1 使用默认主题的

这种情况需要依赖你应用或者Activity使用的主题,我这里使用的主题是继承Theme.AppCompat.Light.DarkActionBar的,其余主题非继承自Theme.AppCompat的可能不行。

例,一个CardView属性设置:

    app:cardBackgroundColor="@color/colorAccent"    app:cardCornerRadius="5dp"    android:focusable="true"    android:clickable="true"    android:foreground="?android:attr/selectableItemBackground"

效果:
android4.4效果:

image

MIUI 10 android9.0效果:

image

3.2 自己设置水波纹

就是自己设置一个Drawable。这里暂且叫card_foreground

为了兼容Android5.0一下,需要建两个card_foreground文件,一个在drawable(兼容Android5.0一下)目录,一个在drawable-v21(给Android5.0及以上平台用)。

例:
drawable/card_foreground.xml

<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android">    <!--点击状态是暗黑色-->    <item android:state_pressed="true" android:drawable="@color/click_true"/>    <!--正常状态透明色-->    <item android:state_pressed="false" android:drawable="@color/transparent"/></selector>

drawable-v21/card_foreground.xml ripple就是波纹

<?xml version="1.0" encoding="utf-8"?><ripple xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:color="@color/colorPrimary"    android:drawable="@drawable/card_foreground_selector"    ></ripple>

drawable-v21/card_foreground_selector.xml

<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android">    <item android:state_pressed="true">        <shape android:shape="rectangle">            <solid android:color="#18ffc400"/>        </shape>    </item>    <item android:state_focused="true"        android:state_enabled="true">        <shape android:shape="rectangle">            <solid android:color="#0f000000"/>        </shape>    </item></selector>

最后,cardView设置属性:

    app:cardBackgroundColor="@color/colorAccent"    app:cardCornerRadius="5dp"    android:focusable="true"    android:clickable="true"    android:foreground="@drawable/card_foreground"

Android4.4 效果: 效果的话边上多了点间距,可以考虑用把selector嵌入到inset设置为前景

image

MIUI 10 android9.0效果:

image

说明
1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
2. 分享目的仅供大家学习和交流,您必须在下载后24小时内删除!
3. 不得使用于非法商业用途,不得违反国家法律。否则后果自负!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是摆设,本站源码仅提供给会员学习使用!
7. 如遇到加密压缩包,请使用360解压,如遇到无法解压的请联系管理员
开心源码网 » Android Marterial UI 之水波纹效果

发表回复