ImageView使用一张图片做出selector效果,控件点击出现波纹效果,友好的交互。

1,波纹效果,因为只有android5.0以上才有效果,所以需要在res下新建drawable-v21文件夹,

首先新建 ripple_bg.xml ,注意是ripple标签

<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
        android:color="#cfd8dc">  //波纹的颜色

    <item android:drawable="@drawable/bg_rectangle"/>	//背景色
</ripple>


下面是背景色  bg_rectangle.xml 也是在drawable-v21文件夹下

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
    <solid android:color="#FFFFFF" />
    
    <!--shape的很多属性随意设置-->
    <!--<corners android:radius="5dp"/> -->
</shape>

最后在用的地方:

<RelativeLayout
                android:layout_width="match_parent"
                android:background="@drawable/ripple_bg"
                android:layout_height="50dp">

            </RelativeLayout>

如果要适配5.0以下,还需要在drawable文件下建一个同样的ripple.xml文件名跑在5.0以下机器才不会报错

<?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="#cfd8dc"/>
        </shape>
    </item>

    <item android:drawable="@color/white" />  //颜色属性随意
</selector>


ImageView想要不错效果的偷懒专用。

1,在drawable下新建scan_image.xml   

<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
        android:src="@mipmap/ic_scan_new" //图片文件
        android:tint="#ea0e24"			//合并颜色
        android:tintMode="multiply">		//合并模式

</bitmap>


2,创建selector   scan_selector_bg.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@drawable/scan_image" android:state_pressed="true"/>

    <item android:drawable="@mipmap/ic_scan_new"/>

</selector>

最后在用的地方

<ImageView
            android:src="@drawable/scan_selector_bg"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>


这样的ImageView就自带selector效果了,点击的时候就会变色。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值