Android5.x自定义水波效果

Android 5.x中新增了RippleDrawable类型,即波纹效果.效果图如下:
这里写图片描述

在res中添加一个新的文件夹drawable-v21,用于保存波纹效果

ripple.xml

<?xml version="1.0" encoding="utf-8"?>

<ripple xmlns:android="http://schemas.android.com/apk/res/android"
    android:color="#ff4cA3D2"
    android:background="?android:attr/selectableItemBackground"
    >
    <!--控件正常状态下显示效果-->
    <item android:drawable="@drawable/bg"/>

</ripple>

drawabel/bg.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#fff"/>
</shape>
  • android:radius:设置波纹半径大小,单位为px
  • android:color:设置指定波纹效果的颜色。而且必须为”#rgb”, “#argb”, “#rrggbb”, or “#aarrggbb” 格式。
  • android:background=”?android:attr/selectableItemBackground”波纹有边界
  • android:background=”?android:attr/selectableItemBackgroundBorderless”波纹超出边界

或将两个xml写成一个

<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
    android:color="#ff4cA3D2"
    android:background="?android:attr/selectableItemBackground"
    >
    <item >
        <shape>
            <solid android:color="#fff"/>
        </shape>
    </item>
</ripple>

本身RippleDrawable继承android.graphics.drawable.LayerDrawable,所以支持LayerDrawable操作。
RippleDrawable应用于ImageView没有出现水波效果

自定义水波纹形状

官方文档中提到,触摸反馈绘制可能包含多个子层,包括未绘制到屏幕上一个特殊的遮罩层。该遮蔽层android:id值为 “@android:id/mask”,即水波纹本身这一层。
drawable/ripple2.xml

<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
    android:color="#ff4cA3D2"
    android:background="?android:attr/selectableItemBackground"
    >
    <item android:id="@android:id/mask"
        android:drawable="@mipmap/ic_launcher"/>
</ripple>

这里使用系统自带的小图标。ic_launcher为一张图片,也可使用ShapeDrawable改变水波纹的形状。

补充

5.0后,系统默认为应用View控件添加波纹效果。只需要在应用主题中增加并修改android:colorControlHighlight属性就能修改整个应用的波纹效果颜色。
values/styles.xml

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        <!--添加并修改该属性-->
        <item name="colorControlHighlight">#ff4cA3D2</item>
    </style>
</resources>

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.brazen_zz.rippledemo">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

可以为控件设置android:background属性设置波纹边界。

相关文章

官方文档API:https://developer.android.com/reference/android/graphics/drawable/RippleDrawable.html
demo下载:https://github.com/Abrazen/RippleDemo(比较简单,以后慢慢增加更酷炫的效果)。

  • 7
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值