当我开始接触Tint
这个词的时候,实际上是蛮不理解它的意思的,以及并不清楚Google发明它的目的,它通常搭配Background
配合使用,可是如今已经有了Background
,为何还须要Tint
呢?java
Tint 翻译为
着色
。android
着色,着什么色呢?和背景有关,固然是着背景的色。当我开发客户端,使用了appcompat-v7
包的时候,为了实现Material Design
的效果,咱们会去设置主题里的几个颜色,重要的好比primaryColor
,colorControlNormal
,colorControlActived
等等,而咱们使用的一些组件,好比EditText
就会自动变成咱们想要的背景颜色,在背景图只有一张的状况下,这样的作法极大的减小了咱们apk包的大小。git
实现的方式就是用一个颜色为咱们的背景图片设置tint
(着色)。github
看看即将发布的SegmentFault for Android 2.7
中,发布问题功能,这个EditText的颜色和咱们的主要颜色相同。它利用了TintManager
这个类,为本身的背景进行着色(绿色)。
那么这个原始图是什么样子呢?咱们从appcompat-v7
包中找到了这个图,是一个.9
图,样子以下:app
其实它只是一个黑色的条,经过绿色的着色,变成了一个绿色的条。 就是这样的设计方式,使得咱们在Material Design
中省了多少资源文件呀!ide
好了,既然理解了tint
的含义,咱们赶忙看下这一切是如何实现的吧。
其实底层特别简单,了解过渲染的同窗应该知道PorterDuffColorFilter
这个东西,咱们使用SRC_IN
的方式,对这个Drawable进行颜色方面的渲染,就是在这个Drawable中有像素点的地方,再用咱们的过滤器着色一次。
实际上若是要咱们本身实现,只用获取View
的backgroundDrawable
以后,设置下colorFilter
便可。函数
看下最核心的代码就这么几行ui
javaif (filter == null) { // Cache miss, so create a color filter and add it to the cache filter = new PorterDuffColorFilter(color, mode); } d.setColorFilter(filter);
一般状况下,咱们的mode通常都是SRC_IN
,若是想了解这个属性相关的资料,这里是传送门: http://blog.csdn.net/t12x3456/article/details/10432935 (中文)this
因为API Level 21
之前不支持background tint
在xml中设置,因而提供了ViewCompat.setBackgroundTintList
方法和ViewCompat.setBackgroundTintMode
用来手动更改须要着色的颜色,但要求相关的View继承TintableBackgroundView
接口。spa
看下源码是如何实现的吧,咱们以AppCompatEditText
为例:
看下构造函数(省略无关代码)
javapublic AppCompatEditText(Context context, AttributeSet attrs, int defStyleAttr) { super(TintContextWrapper.wrap(context), attrs, defStyleAttr); ... ColorStateList tint = a.getTintManager().getTintList(a.getResourceId(0, -1)); //根据背景的resource id获取内置的着色颜色。 if (tint != null) { setInternalBackgroundTint(tint); //设置着色 } ... } private void setInternalBackgroundTint(ColorStateList tint) { if (tint != null) { if (mInternalBackgroundTint == null) { mInternalBackgroundTint = new TintInfo(); } mInternalBackgroundTint.mTintList = tint; mInternalBackgroundTint.mHasTintList = true; } else { mInternalBackgroundTint = null; } //上面的代码是记录tint相关的信息。 applySupportBackgroundTint(); //对背景应用tint } private void applySupportBackgroundTint() { if (getBackground() != null) { if (mBackgroundTint != null) { TintManager.tintViewBackground(this, mBackgroundTint); } else if (mInternalBackgroundTint != null) { TintManager.tintViewBackground(this, mInternalBackgroundTint); //最重要的,对tint进行应用 } } }
而后咱们进入tintViewBackground
看下TintManager
里面的源码
javapublic static void tintViewBackground(View view, TintInfo tint) { final Drawable background = view.getBackground(); if (tint.mHasTintList) { //若是设置了tint的话,对背景设置PorterDuffColorFilter setPorterDuffColorFilter( background, tint.mTintList.getColorForState(view.getDrawableState(), tint.mTintList.getDefaultColor()), tint.mHasTintMode ? tint.mTintMode : null); } else { background.clearColorFilter(); } if (Build.VERSION.SDK_INT <= 10) { // On Gingerbread, GradientDrawable does not invalidate itself when it's ColorFilter // has changed, so we need to force an invalidation view.invalidate(); } } private static void setPorterDuffColorFilter(Drawable d, int color, PorterDuff.Mode mode) { if (mode == null) { // If we don't have a blending mode specified, use our default mode = DEFAULT_MODE; } // First, lets see if the cache already contains the color filter PorterDuffColorFilter filter = COLOR_FILTER_CACHE.get(color, mode); if (filter == null) { // Cache miss, so create a color filter and add it to the cache filter = new PorterDuffColorFilter(color, mode); COLOR_FILTER_CACHE.put(color, mode, filter); } // 最最重要,原来是对background drawable设置了colorFilter 完成了咱们要的功能。 d.setColorFilter(filter); }
以上是对API21如下的兼容。
若是咱们要实现本身的AppCompat
组件实现tint
的一些特性的话,咱们就能够指定好ColorStateList
,利用TintManager
对本身的背景进行着色,固然须要对外开放设置的接口的话,咱们还要实现TintableBackgroundView
接口,而后用ViewCompat.setBackgroundTintList
进行设置,这样能完成对v7以上全部版本的兼容。
好比我如今要对一个自定义组件实现对Tint
的支持,其实只用继承下,加一些代码就行了,代码以下(几乎通用):
public class AppCompatFlowLayout extends FlowLayout implements TintableBackgroundView { private static final int[] TINT_ATTRS = { android.R.attr.background }; private TintInfo mInternalBackgroundTint; private TintInfo mBackgroundTint; private TintManager mTintManager; public AppCompatFlowLayout(Context context) { this(context, null); } public AppCompatFlowLayout(Context context, AttributeSet attributeSet) { this(context, attributeSet, 0); } public AppCompatFlowLayout(Context context, AttributeSet attributeSet, int defStyle) { super(context, attributeSet, defStyle); if (TintManager.SHOULD_BE_USED) { TintTypedArray a = TintTypedArray.obtainStyledAttributes(getContext(), attributeSet, TINT_ATTRS, defStyle, 0); if (a.hasValue(0)) { ColorStateList tint = a.getTintManager().getTintList(a.getResourceId(0, -1)); if (tint != null) { setInternalBackgroundTint(tint); } } mTintManager = a.getTintManager(); a.recycle(); } } private void applySupportBackgroundTint() { if (getBackground() != null) { if (mBackgroundTint != null) { TintManager.tintViewBackground(this, mBackgroundTint); } else if (mInternalBackgroundTint != null) { TintManager.tintViewBackground(this, mInternalBackgroundTint); } } } @Override protected void drawableStateChanged() { super.drawableStateChanged(); applySupportBackgroundTint(); } private void setInternalBackgroundTint(ColorStateList tint) { if (tint != null) { if (mInternalBackgroundTint == null) { mInternalBackgroundTint = new TintInfo(); } mInternalBackgroundTint.mTintList = tint; mInternalBackgroundTint.mHasTintList = true; } else { mInternalBackgroundTint = null; } applySupportBackgroundTint(); } @Override public void setSupportBackgroundTintList(ColorStateList tint) { if (mBackgroundTint == null) { mBackgroundTint = new TintInfo(); } mBackgroundTint.mTintList = tint; mBackgroundTint.mHasTintList = true; applySupportBackgroundTint(); } @Nullable @Override public ColorStateList getSupportBackgroundTintList() { return mBackgroundTint != null ? mBackgroundTint.mTintList : null; } @Override public void setSupportBackgroundTintMode(PorterDuff.Mode tintMode) { if (mBackgroundTint == null) { mBackgroundTint = new TintInfo(); } mBackgroundTint.mTintMode = tintMode; mBackgroundTint.mHasTintMode = true; applySupportBackgroundTint(); } @Nullable @Override public PorterDuff.Mode getSupportBackgroundTintMode() { return mBackgroundTint != null ? mBackgroundTint.mTintMode : null; } }
赶快去试试吧~