Tint Drawable Resources
With Android 5.0 (API level 21) and above, you can tint bitmaps and nine-patches defined as alpha masks. You can tint them with color resources or theme attributes that resolve to color resources (for example, ?android:attr/colorPrimary). Usually, you create these assets only once and color them automatically to match your theme.

You can apply a tint to BitmapDrawable or NinePatchDrawable objects with the setTint() method. You can also set the tint color and mode in your layouts with the android:tint and android:tintMode attributes.

Drawable 的染色,遮罩或者绘制。 在5.0以后我们就可以为bitmap或者是9-patch定义一个透明的遮罩。
BitmapDrawable 和NinePatchDrawable使用setTint()方法.(是drawable的方法不是控件的)
而在xml文件中使用android:tint和android:tintMode这两个属性。

注意点: 使用android:tint指定颜色时一定要带透明度。#50ff00ff也就是说是8位的色值而不是6位的

属性说明
android:tint: 设置的是颜色
android:tintMode:设置的是类型(src_in,src_over,src_atop,add,screen,multiply)
类型说明
src_in 只显示设置的遮罩颜色。 相当于遮罩在里面。
src_over遮罩颜色和图片都显示。相当于遮罩在图片上方。(特别是色值带透明度的)
src_atop遮罩在图片上方
multiply 混合色遮罩
screen
add 混合遮罩,drawable颜色和透明度。

官网说明:src_over:   3    The tint is drawn on top of the drawable. [Sa + (1 - Sa)*Da, Rc = Sc + (1 - Sa)*Dc]src_in: 5    The tint is masked by the alpha channel of the drawable. The drawable’s color channels are thrown out. [Sa * Da, Sc * Da]src_atop:   9    The tint is drawn above the drawable, but with the drawable’s alpha channel masking the result. [Da, Sc * Da + (1 - Sa) * Dc]multiply:   14   Multiplies the color and alpha channels of the drawable with those of the tint. [Sa * Da, Sc * Dc]screen: 15   [Sa + Da - Sa * Da, Sc + Dc - Sc * Dc]add:    16   Combines the tint and drawable color and alpha channels, clamping the result to valid color values. Saturate(S + D)

代码设置:
1,ImageView
mImage.setColorFilter(mHintColor, mMode);
2,Button,TextView等
tv.setBackgroundTintList(ColorStateList.valueOf(Color.parseColor(“#ff00ff”)));
tv.setBackgroundTintMode(Mode.SRC_OVER);

xml布局文件: 原图和设置tint后的对比。

 <ImageView            android:layout_width="100dp"            android:layout_height="100dp"            android:scaleType="centerCrop"            android:src="@drawable/zly1" />        <ImageView            android:id="@+id/imageView1"            android:layout_width="100dp"            android:layout_height="100dp"            android:scaleType="centerCrop"            android:src="@drawable/zly1"            android:tint="#90ff00ff"            android:tintMode="multiply" />        <TextView            android:id="@+id/textView1"            android:layout_width="100dp"            android:layout_height="100dp"            android:layout_gravity="center_vertical"            android:layout_marginLeft="10dip"            android:background="@drawable/zly1"            android:backgroundTint="#90ff00ff"            android:backgroundTintMode="multiply"            android:text="tint_tintMode"            android:textColor="#00ffff" />

效果图:
src_over效果
android:tint_第1张图片

multiply效果
android:tint_第2张图片

这几个属性感觉和xfremode非常相似:

private static final Xfermode[] sModes = {            new PorterDuffXfermode(PorterDuff.Mode.CLEAR),            new PorterDuffXfermode(PorterDuff.Mode.SRC),            new PorterDuffXfermode(PorterDuff.Mode.DST),            new PorterDuffXfermode(PorterDuff.Mode.SRC_OVER),            new PorterDuffXfermode(PorterDuff.Mode.DST_OVER),            new PorterDuffXfermode(PorterDuff.Mode.SRC_IN),            new PorterDuffXfermode(PorterDuff.Mode.DST_IN),            new PorterDuffXfermode(PorterDuff.Mode.SRC_OUT),            new PorterDuffXfermode(PorterDuff.Mode.DST_OUT),            new PorterDuffXfermode(PorterDuff.Mode.SRC_ATOP),            new PorterDuffXfermode(PorterDuff.Mode.DST_ATOP),            new PorterDuffXfermode(PorterDuff.Mode.XOR),            new PorterDuffXfermode(PorterDuff.Mode.DARKEN),            new PorterDuffXfermode(PorterDuff.Mode.LIGHTEN),            new PorterDuffXfermode(PorterDuff.Mode.MULTIPLY),            new PorterDuffXfermode(PorterDuff.Mode.SCREEN)        };

对应的效果图:
android:tint_第3张图片

更多相关文章

  1. Android 字体颜色渐变效果 Span实现
  2. android图片缩放手势检测类--ScaleGestureDetector
  3. android图片压缩方法
  4. android ListView SimpleAdapter 带图片
  5. Android之OnGestureListener实现图片的左右滑动
  6. Android ListView(Selector 颜色)
  7. 图片压缩保存读取操作
  8. Android 圆角图片,基于Glide4.9 的 BitmapTransformation,可任意设
  9. 【原创】Android 4.4前后版本读取图库图片方式的变化

随机推荐

  1. Android(安卓)启动过程分析 (一)
  2. Android(安卓)Studio 快捷键大全
  3. LeadTools Android(安卓)入门教学——运
  4. 布局资源(layout)的简单使用
  5. Android中Style和Theme的使用总结
  6. Android高手进阶教程(二)之----Android(
  7. Chris:怎样成为一名Android应用开发者
  8. Android中几种图像特效处理的集锦!!
  9. Android的图表世界–如何使用MPAndroidCh
  10. Android和H5互调案例基础详解