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"/>