Android-使用tint一张图制作selector

CarMarler 8年前
   <p>Android 有时候制作按下的效果挺麻烦的,得放色值不同的两张图,这个就比较尴尬了,明明是相同的资源。现在Android Material Design 中提供了一个东西:Tint,一张矢量图是能适配所有的颜色。</p>    <h2><strong>首先这个东西可以直接对 ImageView 上色渲染</strong></h2>    <p>如下,相同的两个图,使用 tint 改变颜色:</p>    <pre>  <code class="language-java"><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" /></code></pre>    <h2><strong>使用 tint 制作selector</strong></h2>    <h2><strong>以下为成品的 selector,selector_bg.xml:</strong></h2>    <pre>  <code class="language-java"><?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></code></pre>    <p>其中btn_begin为png图片,而btn_begin_pressed则是使用此图进行tint着色后的xml文件.</p>    <h2><strong>btn_begin_pressed.xml:</strong></h2>    <pre>  <code class="language-java"><?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></code></pre>    <p>android:tint: 设置的是颜色</p>    <p>android:tintMode:设置的是类型(src_in,src_over,src_atop,add,screen,multiply)</p>    <p>各个属性效果如图:</p>    <p style="text-align:center"><img src="https://simg.open-open.com/show/864477476bfabcb43cfbb39807576b41.png"></p>    <p style="text-align:center">tinMode</p>    <p>然后不管在xml布局中android:background,还是java代码setBackgroundResource,都可以直接使用成品背景图了</p>    <h2><strong>改变MD下的 EditText 背景色和光标颜色</strong></h2>    <p>为了实现 Material Design的效果,使用的主题里的颜色配置比如 primaryColor,colorControlNormal,colorControlActived什么的会让 EditText 自动适配背景颜色,</p>    <p>其实现方式就是使用了 tint ,如下图:</p>    <p style="text-align:center"><img src="https://simg.open-open.com/show/131a47dc9cdb7e0f9c69ad9b8a7d2d60.gif"></p>    <p style="text-align:center">随便举个栗子</p>    <p>先不用在意那个 TextInputLayout,看背景色,对就是那条线./抠鼻~</p>    <p>然后我们不想用这个颜色怎么办捏?</p>    <p>对于那条线可以直接设置:</p>    <pre>  <code class="language-java">android:backgroundTint="@color/颜色"</code></pre>    <p>额,那个光标也想改了??需要新建一个 cursor_shape.xml:</p>    <pre>  <code class="language-java"><?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></code></pre>    <p>然后就可以设置:</p>    <pre>  <code class="language-java">android:textCursorDrawable="@drawable/cursor_shape"</code></pre>    <h2><strong>关于 tint 的源码分析参考:</strong></h2>    <p><a href="http://www.open-open.com/lib/view/open1438438952410.html">谈谈Android Material Design 中的Tint(着色)</a></p>    <h2><strong>再说点儿</strong></h2>    <p>说到了 tint 着色,不得不提一下Android5.0加入的取色器:Palette,可以根据Bitmap显示出来的东西提取颜色,然后到处 set,达到背景或者 toolBar 和图片达到和谐,看看效果:</p>    <p style="text-align:center"><img src="https://simg.open-open.com/show/57fbacb49a38fac1337188dc72ee04d7.gif"></p>    <p style="text-align:center">MD大法好</p>    <p> </p>    <p> </p>    <p>来自:http://www.jianshu.com/p/9c5baee9da4c</p>    <p> </p>