当前位置: 首页 > 科技观察

谈谈AndroidMaterialDesign中的Tint

时间:2023-03-21 00:54:51 科技观察

什么是Tint?刚开始接触Tint这个词的时候,我不是很明白它的意思,也不知道谷歌发明它的目的是什么。它通常与Background结合使用,但是既然有了Background,为什么还需要Tint?Tint翻译为着色。着色,什么颜色?跟背景有关,当然是背景的颜色。我在开发客户端的时候,使用了appcompat-v7包,为了达到MaterialDesign的效果,我们会在主题中设置几种颜色,比如primaryColor,colorControlNormal,colorControlActived等,而我们使用的一些组件,对于例如EditText会自动改变成我们想要的背景色。在只有一张背景图片的情况下,这种做法大大减小了我们apk包的体积。实现它的方法是使用颜色为我们的背景图像设置色调(着色)。示例:看看即将发布的SegmentFaultforAndroid2.7,发布问题函数,这个EditText的颜色和我们的主色一样。它使用TintManager类为其自己的背景着色(绿色)。那么这个原始图像是什么样的呢?我们从appcompat-v7包里找到这个图,是一个.9的图,长这样:其实就是一个黑条,被涂成绿色变成了绿条。正是这种设计方式,让我们在MaterialDesign中节省了大量的资源文件!好吧,既然我们了解了tint的含义,让我们快速看看这一切是如何实现的。其实底层很简单。学过渲染的同学应该知道PorterDuffColorFilter。我们用SRC_IN来渲染这个Drawable的颜色,也就是这个Drawable里面有像素的地方,我们再用我们的filter给它上色。.其实如果我们想自己实现的话,只需要在获取到View的backgroundDrawable之后设置colorFilter即可。看核心代码,几行就可以了想知道这个属性相关的信息,传送门在这里:http://blog.csdn.net/t12x3456/article/details/10432935(中文)由于APILevel21之前不支持在xml中设置背景色,所以提供了ViewCompat.setBackgroundTintList方法和ViewCompat.setBackgroundTintMode方法来手动改变需要着色的颜色,但是相关的View需要继承TintableBackgroundView接口。源码分析下面我们来看看源码是如何实现的。我们以AppCompatEditText为例:看构造函数(省略无关代码)publicAppCompatEditText(Contextcontext,AttributeSetattrs,intdefStyleAttr){super(TintContextWrapper.wrap(context),attrs,defStyleAttr);。..ColorStateListtint=a.getTintManager().getTintList(a.getResourceId(0,-1));//根据背景的resourceid获取内置着色颜色。if(tint!=null){setInternalBackgroundTint(tint);//设置颜色}...}privatevoidsetInternalBackgroundTint(ColorStateListtint){if(tint!=null){if(mInternalBackgroundTint==null){mInternalBackgroundTint=newTintInfo();}mInternalBackgroundTint.mTintList=tint;mInternalBackgroundTint.mHasTintList=true;}else{mInternalBackgroundTint=null;}//上面的代码是记录tint相关的信息。applySupportBackgroundTint();//对背景应用色调}privatevoidapplySupportBackgroundTint(){if(getBackground()!=null){if(mBackgroundTint!=null){TintManager.tintViewBackground(this,mBackgroundTint);}elseif(mInternalBackgroundTint!=null){TintManager.tintViewBackground(this,mInternalBackgroundTint);//最重要的是应用tint}}}然后我们进入tintViewBackground,在TintManager中查看源码publicstaticvoidtintViewBackground(Viewview,TintInfotint){finalDrawablebackground=view.getBackground();if(tint.mHasTintList){//如果设置了tint,设置PorterDuffColorFiltersetPorterDuffColorFilter(background,tint.mTintList.getColorForState(view.getDrawableState(),tint.mTintList.getDefaultColor()),tint.mHasTintMode?tint.mTintMode:null);}else{background.clearColorFilter();}if(Build.VERSION.SDK_INT<=10){//OnGingerbread,GradientDrawabledoesnotinvalidateitselfwhenit'sColorFilter//haschanged,soweneedtoforceaninvalidationview.invalidate();}}privatestaticvoidsetPorterDuffColorFilter(DrawableDrawable.调制解调器ode){if(mode==null){//Ifwedon'thaveableendingmodespecified,useourdefaultmode=DEFAULT_MODE;}//首先,让我们看看缓存是否已经包含了colorfilterPorterDuffColorFilterfilter=COLOR_FILTER_CACHE.get(color,mode);if(tofilter==null)lor,/socreatedcomiss=newPorterDuffColorFilter(color,mode);COLOR_FILTER_CACHE.put(color,mode,filter);}//最重要的是在backgrounddrawable上设置colorFilter来完成我们想要的功能d.setColorFilter(filter);}以上是API21以下的正确兼容性。如果我们想实现自己的AppCompat组件来实现tint的一些特性,我们可以指定ColorStateList,使用TintManager来给自己的背景上色。当然,如果我们需要对外开放设置的接口,我们还需要实现TintableBackgroundView接口,然后使用ViewCompat.setBackgroundTintList进行设置,可以完成对v7以上所有版本的兼容。例如,我想为自定义组件实现Tint支持。其实我只需要继承和添加一些代码即可。代码如下(几乎通用):背景};privateTintInfoInternalBackgroundTint;privateTintInfomBackgroundTint;privateTintManagermTintManager;publicAppCompatFlowLayout(Contextcontext){this(context,null);}publicAppCompatFlowLayout(Contextcontext,AttributeSetattributeSet){this(context,attributeSet,0);}publicAppCompatFlowLayout(Contextcontext,AttributeSetattributeSet,intdefStyle){super(context,attributeSet,defStyle);if(TintManager.SHOULD_BE_USED){TintTypedArraya=TintTypedArray.obtainStyledAttributes(getContext(),attributeSet,TINT_ATTRS,defStyle,0);if(a.hasValue(0)){ColorStateListtint=a.getTintManager().getTintList(a.getResourceId(0,-1));if(tint!=null){setInternalBackgroundTint(tint);}