日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 运维知识 > Android >内容正文

Android

Android L 的 Tint(着色)

發(fā)布時(shí)間:2023/12/10 Android 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android L 的 Tint(着色) 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

Tint 是什么?

Tint 翻譯為著色。

著色,著什么色呢,和背景有關(guān)?當(dāng)然是著背景的色。當(dāng)我們開發(fā) App 的時(shí)候,如果使用了 Theme.AppCompat 主題的時(shí)候,會(huì)發(fā)現(xiàn) ActionBar 或者 Toolbar 及相應(yīng)的控件的顏色會(huì)相應(yīng)的變成我們?cè)?Theme 中設(shè)置的 colorPrimary, colorPrimaryDark, colorAccent 這些顏色,這是為什么呢,這就全是 Tint 的功勞了!

這樣做有什么好處呢?好處就是你不必再老老實(shí)實(shí)的打開 PS 再制作一張新的資源圖。而且大家都知道 apk 包最大的就是圖片資源了,這樣減少不必要資源圖,可以極大的減少了我們的 apk 包的大小。

實(shí)現(xiàn)的方式就是用一個(gè)顏色為我們的背景圖片設(shè)置 Tint(著色)。


看看即將發(fā)布的SegmentFault for Android 2.7中,發(fā)布問(wèn)題功能,這個(gè)EditText的顏色和我們的主要顏色相同。它利用了TintManager這個(gè)類,為自己的背景進(jìn)行著色(綠色)。
那么這個(gè)原始圖是什么樣子呢?我們從appcompat-v7包中找到了這個(gè)圖,是一個(gè).9圖,樣子如下:


其實(shí)它只是一個(gè)黑色的條,通過(guò)綠色的著色,變成了一個(gè)綠色的條。 就是這樣的設(shè)計(jì)方式,使得我們?cè)贛aterial Design中省了多少資源文件呀!


例子:

大家可以看上面再?gòu)垐D,這個(gè)是做的一個(gè)應(yīng)用“小白球”,如圖 1 的小圖片本來(lái)都是白色的(圓背景的單獨(dú)設(shè)的),但是經(jīng)過(guò) Tint 著色后就變成了圖 2 中的淺藍(lán)色了。

好了,既然理解了tint的含義,我們趕緊看下這一切是如何實(shí)現(xiàn)的吧。?
其實(shí)底層特別簡(jiǎn)單,了解過(guò)渲染的同學(xué)應(yīng)該知道PorterDuffColorFilter這個(gè)東西,我們使用SRC_IN的方式,對(duì)這個(gè)Drawable進(jìn)行顏色方面的渲染,就是在這個(gè)Drawable中有像素點(diǎn)的地方,再用我們的過(guò)濾器著色一次。?
實(shí)際上如果要我們自己實(shí)現(xiàn),只用獲取View的backgroundDrawable之后,設(shè)置下colorFilter即可。

看下最核心的代碼就這么幾行

if (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,如果想了解這個(gè)屬性相關(guān)的資料,這里是傳送門:http://blog.csdn.net/t12x3456/article/details/10432935?(中文)

由于API Level 21以前不支持background tint在xml中設(shè)置,于是提供了ViewCompat.setBackgroundTintList方法和ViewCompat.setBackgroundTintMode用來(lái)手動(dòng)更改需要著色的顏色,但要求相關(guān)的View繼承TintableBackgroundView接

源碼解析

以 EditText 為例,其它的基本一致

public AppCompatEditText(Context context, AttributeSet attrs, int defStyleAttr) {super(TintContextWrapper.wrap(context), attrs, defStyleAttr);...ColorStateList tint = a.getTintManager().getTintList(a.getResourceId(0, -1)); //根據(jù)背景的resource id獲取內(nèi)置的著色顏色。if (tint != null) {setInternalBackgroundTint(tint); //設(shè)置著色}... }private void setInternalBackgroundTint(ColorStateList tint) {if (tint != null) {if (mInternalBackgroundTint == null) {mInternalBackgroundTint = new TintInfo();}mInternalBackgroundTint.mTintList = tint;mInternalBackgroundTint.mHasTintList = true;} else {mInternalBackgroundTint = null;}//上面的代碼是記錄tint相關(guān)的信息。applySupportBackgroundTint(); //對(duì)背景應(yīng)用tint }private void applySupportBackgroundTint() {if (getBackground() != null) {if (mBackgroundTint != null) {TintManager.tintViewBackground(this, mBackgroundTint);} else if (mInternalBackgroundTint != null) {TintManager.tintViewBackground(this, mInternalBackgroundTint); //最重要的,對(duì)tint進(jìn)行應(yīng)用}} }
然后我們進(jìn)入tintViewBackground看下TintManager里面的源碼

public static void tintViewBackground(View view, TintInfo tint) {final Drawable background = view.getBackground();if (tint.mHasTintList) {//如果設(shè)置了tint的話,對(duì)背景設(shè)置PorterDuffColorFiltersetPorterDuffColorFilter(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 invalidationview.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 defaultmode = DEFAULT_MODE;}// First, lets see if the cache already contains the color filterPorterDuffColorFilter filter = COLOR_FILTER_CACHE.get(color, mode);if (filter == null) {// Cache miss, so create a color filter and add it to the cachefilter = new PorterDuffColorFilter(color, mode);COLOR_FILTER_CACHE.put(color, mode, filter);}// 最最重要,原來(lái)是對(duì)background drawable設(shè)置了colorFilter 完成了我們要的功能。d.setColorFilter(filter); }

以上是對(duì)API21以下的兼容。
如果我們要實(shí)現(xiàn)自己的AppCompat組件實(shí)現(xiàn)tint的一些特性的話,我們就可以指定好ColorStateList,利用TintManager對(duì)自己的背景進(jìn)行著色,當(dāng)然需要對(duì)外開放設(shè)置的接口的話,我們還要實(shí)現(xiàn)TintableBackgroundView接口,然后用ViewCompat.setBackgroundTintList進(jìn)行設(shè)置,這樣能完成對(duì)v7以上所有版本的兼容。

實(shí)例

比如我現(xiàn)在要對(duì)一個(gè)自定義組件實(shí)現(xiàn)對(duì)Tint的支持,其實(shí)只用繼承下,加一些代碼就好了,代碼如下(幾乎通用):

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);}}}@Overrideprotected 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();}@Overridepublic void setSupportBackgroundTintList(ColorStateList tint) {if (mBackgroundTint == null) {mBackgroundTint = new TintInfo();}mBackgroundTint.mTintList = tint;mBackgroundTint.mHasTintList = true;applySupportBackgroundTint();}@Nullable@Overridepublic ColorStateList getSupportBackgroundTintList() {return mBackgroundTint != null ? mBackgroundTint.mTintList : null;}@Overridepublic void setSupportBackgroundTintMode(PorterDuff.Mode tintMode) {if (mBackgroundTint == null) {mBackgroundTint = new TintInfo();}mBackgroundTint.mTintMode = tintMode;mBackgroundTint.mHasTintMode = true;applySupportBackgroundTint();}@Nullable@Overridepublic PorterDuff.Mode getSupportBackgroundTintMode() {return mBackgroundTint != null ? mBackgroundTint.mTintMode : null;} }
  • Github:https://github.com/iQuick/AndroidTint


總結(jié)

以上是生活随笔為你收集整理的Android L 的 Tint(着色)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。