Android-使用tint一張圖制作selector

CarMarler 8年前發布 | 12K 次閱讀 安卓開發 Android開發 移動開發

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

 

 本文由用戶 CarMarler 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!