Android-使用tint一张图制作selector

Android 有时候制作按下的效果挺麻烦的,得放色值不同的两张图,这个就比较尴尬了,明明是相同的资源。现在Android Material Design 中提供了一个东西:Tint,一张矢量图是能适配所有的颜色。

首先这个东西可以直接对 ImageView 上色渲染

如下,相同的两个图,使用 tint 改变颜色:

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="10dp"
    android:src="@mipmap/ic_launcher" />

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="10dp"
    android:src="@mipmap/ic_launcher"
    android:tint="#FFF" />

使用 tint 制作selector

以下为成品的 selector,selector_bg.xml:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/    apk/res/android">
    <item android:drawable="@drawable/btn_begin_pressed" android:state_pressed="true">
    </item>
    <item android:drawable="@drawable/btn_begin">
    </item>
</selector>

其中btn_begin为png图片,而btn_begin_pressed则是使用此图进行tint着色后的xml文件.

btn_begin_pressed.xml:

<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
    android:src="@drawable/btn_begin_down"
    android:tint="#7b5b5b"
    android:tintMode="multiply">
</bitmap>

android:tint: 设置的是颜色

android:tintMode:设置的是类型(src_in,src_over,src_atop,add,screen,multiply)

各个属性效果如图:

tinMode

然后不管在xml布局中android:background,还是java代码setBackgroundResource,都可以直接使用成品背景图了

改变MD下的 EditText 背景色和光标颜色

为了实现 Material Design的效果,使用的主题里的颜色配置比如 primaryColor,colorControlNormal,colorControlActived什么的会让 EditText 自动适配背景颜色,

其实现方式就是使用了 tint ,如下图:

随便举个栗子

先不用在意那个 TextInputLayout,看背景色,对就是那条线./抠鼻~

然后我们不想用这个颜色怎么办捏?

对于那条线可以直接设置:

android:backgroundTint="@color/颜色"

额,那个光标也想改了??需要新建一个 cursor_shape.xml:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
        <size android:width="1dp" />
    <solid android:color="@color/颜色" />
</shape>

然后就可以设置:

android:textCursorDrawable="@drawable/cursor_shape"

关于 tint 的源码分析参考:

谈谈Android Material Design 中的Tint(着色)

再说点儿

说到了 tint 着色,不得不提一下Android5.0加入的取色器:Palette,可以根据Bitmap显示出来的东西提取颜色,然后到处 set,达到背景或者 toolBar 和图片达到和谐,看看效果:

MD大法好

 

 

来自:http://www.jianshu.com/p/9c5baee9da4c

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值