成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

談談Android Material Design 中的Tint

移動開發 Android
當我開始接觸Tint這個詞的時候,其實是蠻不理解它的意思的,以及并不清楚Google發明它的目的,它一般搭配Background配合使用,但是現在已經有了Background,為什么還需要Tint呢?

什么是Tint

當我開始接觸Tint這個詞的時候,其實是蠻不理解它的意思的,以及并不清楚Google發明它的目的,它一般搭配Background配合使用,但是現在已經有了Background,為什么還需要Tint呢?

Tint 翻譯為著色。

著色,著什么色呢?和背景有關,當然是著背景的色。當我開發客戶端,使用了appcompat-v7包的時候,為了實現Material Design的效果,我們會去設置主題里的幾個顏色,重要的比如primaryColor,colorControlNormal,colorControlActived 等等,而我們使用的一些組件,比如EditText就會自動變成我們想要的背景顏色,在背景圖只有一張的情況下,這樣的做法極大的減少了我們apk包的大小。

實現的方式就是用一個顏色為我們的背景圖片設置tint(著色)。
例子:

 

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


其實它只是一個黑色的條,通過綠色的著色,變成了一個綠色的條。 就是這樣的設計方式,使得我們在Material Design中省了多少資源文件呀!

好了,既然理解了tint的含義,我們趕緊看下這一切是如何實現的吧。
其實底層特別簡單,了解過渲染的同學應該知道PorterDuffColorFilter這個東西,我們使用SRC_IN的方式,對這個Drawable進行顏色方面的渲染,就是在這個Drawable中有像素點的地方,再用我們的過濾器著色一次。
實際上如果要我們自己實現,只用獲取View的backgroundDrawable之后,設置下colorFilter即可。

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

 

  1. if (filter == null) { 
  2. // Cache miss, so create a color filter and add it to the cache 
  3. filter = new PorterDuffColorFilter(color, mode); 
  4.  
  5. d.setColorFilter(filter); 

 

通常情況下,我們的mode一般都是SRC_IN,如果想了解這個屬性相關的資料,這里是傳送門: http://blog.csdn.net/t12x3456/article/details/10432935 (中文)

由于API Level 21以前不支持background tint在xml中設置,于是提供了ViewCompat.setBackgroundTintList方法和ViewCompat.setBackgroundTintMode用來手動更改需要著色的顏色,但要求相關的View繼承TintableBackgroundView接口。
源碼解析

看下源碼是如何實現的吧,我們以AppCompatEditText為例:
看下構造函數(省略無關代碼)

 

  1. public AppCompatEditText(Context context, AttributeSet attrs, int defStyleAttr) { 
  2. super(TintContextWrapper.wrap(context), attrs, defStyleAttr); 
  3.  
  4. ... 
  5.  
  6. ColorStateList tint = a.getTintManager().getTintList(a.getResourceId(0, -1)); //根據背景的resource id獲取內置的著色顏色。 
  7. if (tint != null) { 
  8. setInternalBackgroundTint(tint); //設置著色 
  9.  
  10. ... 
  11.  
  12. private void setInternalBackgroundTint(ColorStateList tint) { 
  13. if (tint != null) { 
  14. if (mInternalBackgroundTint == null) { 
  15. mInternalBackgroundTint = new TintInfo(); 
  16. mInternalBackgroundTint.mTintList = tint; 
  17. mInternalBackgroundTint.mHasTintList = true
  18. else { 
  19. mInternalBackgroundTint = null
  20. //上面的代碼是記錄tint相關的信息。 
  21. applySupportBackgroundTint(); //對背景應用tint 
  22.  
  23.  
  24. private void applySupportBackgroundTint() { 
  25. if (getBackground() != null) { 
  26. if (mBackgroundTint != null) { 
  27. TintManager.tintViewBackground(this, mBackgroundTint); 
  28. else if (mInternalBackgroundTint != null) { 
  29. TintManager.tintViewBackground(this, mInternalBackgroundTint); //最重要的,對tint進行應用 
  30.  
  31. 然后我們進入tintViewBackground看下TintManager里面的源碼 
  32.  
  33. public static void tintViewBackground(View view, TintInfo tint) { 
  34. final Drawable background = view.getBackground(); 
  35. if (tint.mHasTintList) { 
  36. //如果設置了tint的話,對背景設置PorterDuffColorFilter 
  37. setPorterDuffColorFilter( 
  38. background, 
  39. tint.mTintList.getColorForState(view.getDrawableState(), 
  40. tint.mTintList.getDefaultColor()), 
  41. tint.mHasTintMode ? tint.mTintMode : null); 
  42. else { 
  43. background.clearColorFilter(); 
  44.  
  45. if (Build.VERSION.SDK_INT <= 10) { 
  46. // On Gingerbread, GradientDrawable does not invalidate itself when it's ColorFilter 
  47. // has changed, so we need to force an invalidation 
  48. view.invalidate(); 
  49.  
  50.  
  51. private static void setPorterDuffColorFilter(Drawable d, int color, PorterDuff.Mode mode) { 
  52. if (mode == null) { 
  53. // If we don't have a blending mode specified, use our default 
  54. mode = DEFAULT_MODE; 
  55.  
  56. // First, lets see if the cache already contains the color filter 
  57. PorterDuffColorFilter filter = COLOR_FILTER_CACHE.get(color, mode); 
  58.  
  59. if (filter == null) { 
  60. // Cache miss, so create a color filter and add it to the cache 
  61. filter = new PorterDuffColorFilter(color, mode); 
  62. COLOR_FILTER_CACHE.put(color, mode, filter); 
  63.  
  64. // 最最重要,原來是對background drawable設置了colorFilter 完成了我們要的功能。 
  65. d.setColorFilter(filter); 

 

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

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

 

  1. public class AppCompatFlowLayout extends FlowLayout implements TintableBackgroundView { 
  2.  
  3. private static final int[] TINT_ATTRS = { 
  4. android.R.attr.background 
  5. }; 
  6.  
  7. private TintInfo mInternalBackgroundTint; 
  8. private TintInfo mBackgroundTint; 
  9. private TintManager mTintManager; 
  10.  
  11. public AppCompatFlowLayout(Context context) { 
  12. this(context, null); 
  13.  
  14. public AppCompatFlowLayout(Context context, AttributeSet attributeSet) { 
  15. this(context, attributeSet, 0); 
  16.  
  17. public AppCompatFlowLayout(Context context, AttributeSet attributeSet, int defStyle) { 
  18. super(context, attributeSet, defStyle); 
  19.  
  20. if (TintManager.SHOULD_BE_USED) { 
  21. TintTypedArray a = TintTypedArray.obtainStyledAttributes(getContext(), attributeSet, 
  22. TINT_ATTRS, defStyle, 0); 
  23. if (a.hasValue(0)) { 
  24. ColorStateList tint = a.getTintManager().getTintList(a.getResourceId(0, -1)); 
  25. if (tint != null) { 
  26. setInternalBackgroundTint(tint); 

 

責任編輯:chenqingxiang 來源: Gemini @ SegmentFault
相關推薦

2017-02-14 13:35:15

AndroidMaterial De動畫

2014-08-07 14:19:46

Material DeGoogle

2014-08-21 15:29:29

Material De概述

2015-07-21 15:02:37

設計扁平

2014-12-08 13:40:10

Material De色彩

2014-12-08 15:03:17

Material De圖像

2014-12-08 14:35:51

Material De真實動作

2014-09-10 10:35:11

Material De設計原則

2017-02-20 16:03:35

Android We谷歌硬件

2014-10-21 15:26:37

Material DeAndroid應用

2014-12-08 14:15:48

Material De字體排版

2014-08-21 15:40:53

Material De真實動作

2014-08-19 16:10:05

Material DeUI設計趨勢

2014-10-27 14:18:06

Material De交互響應

2018-04-25 09:06:32

Chrome瀏覽器語言

2014-08-11 11:19:19

Material De

2014-07-22 10:44:21

Material De

2022-01-20 20:08:38

MaterialpalettesMaterial 3

2017-01-11 19:15:55

Android著色器Tint

2014-07-02 10:26:52

Material DeGoogle
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 亚洲一区二区视频 | 国产精品久久久久久中文字 | 亚洲性网 | 久久久久久久国产 | 久久涩涩 | 久久精品国产一区二区电影 | 久久精品中文字幕 | 国产一级视频在线观看 | 国产羞羞视频在线观看 | 高清av在线 | 国产精品美女久久久久久免费 | 中文字字幕一区二区三区四区五区 | 亚洲综合色网站 | 日韩成人在线观看 | 成人国产精品色哟哟 | 国产一区二区在线免费 | 性欧美精品一区二区三区在线播放 | 特黄特黄a级毛片免费专区 av网站免费在线观看 | 国产欧美精品一区二区三区 | 精品美女在线观看视频在线观看 | 日韩www视频 | 成人毛片网站 | 亚洲精品1区2区3区 91免费看片 | 日本精品视频 | 婷婷久| 性色视频 | 欧美在线日韩 | 91精品国产一区二区三区 | 色综合天天网 | 91网站在线播放 | 久久精品成人一区 | 欧美色性 | 久久久www成人免费无遮挡大片 | 综合婷婷 | 91久久久久久久久久久 | 日韩精品视频在线 | 日本一区二区不卡视频 | 日本中文字幕一区 | 国产精品日韩欧美一区二区三区 | 不卡在线视频 | 成人免费视频观看 |