Android-使用tint一张图制作selector

    xiaoxiao2023-03-25  5

    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

    转载请注明原文地址: https://ju.6miu.com/read-1203974.html
    最新回复(0)