|
1 | 1 | package com.xw.repo; |
2 | 2 |
|
3 | 3 | import android.content.Context; |
| 4 | +import android.content.res.ColorStateList; |
4 | 5 | import android.content.res.Resources; |
5 | 6 | import android.content.res.TypedArray; |
6 | 7 | import android.graphics.Canvas; |
| 8 | +import android.graphics.Color; |
7 | 9 | import android.graphics.ColorFilter; |
8 | 10 | import android.graphics.Paint; |
9 | 11 | import android.graphics.PixelFormat; |
|
36 | 38 | */ |
37 | 39 | public class VectorCompatTextView extends AppCompatCheckedTextView { |
38 | 40 |
|
| 41 | + private static final int DEFAULT_COLOR = Color.BLACK; |
| 42 | + |
39 | 43 | private boolean isTintDrawableInTextColor; |
40 | | - private int mDrawableCompatColor; |
| 44 | + private ColorStateList mDrawableCompatTint; |
41 | 45 | private boolean isDrawableAdjustTextWidth; |
42 | 46 | private boolean isDrawableAdjustTextHeight; |
43 | 47 | private boolean isDrawableAdjustViewWidth; |
@@ -103,7 +107,14 @@ private void initAttrs(Context context, AttributeSet attrs) { |
103 | 107 | } |
104 | 108 |
|
105 | 109 | isTintDrawableInTextColor = a.getBoolean(R.styleable.VectorCompatTextView_tintDrawableInTextColor, false); |
106 | | - mDrawableCompatColor = a.getColor(R.styleable.VectorCompatTextView_drawableCompatColor, -1); |
| 110 | + if (a.hasValue(R.styleable.VectorCompatTextView_drawableCompatTint)) { |
| 111 | + mDrawableCompatTint = a.getColorStateList(R.styleable.VectorCompatTextView_drawableCompatTint); |
| 112 | + } else if (a.hasValue(R.styleable.VectorCompatTextView_drawableCompatColor)) { |
| 113 | + // @deprecated |
| 114 | + // Use drawableCompatTint instead. |
| 115 | + int color = a.getColor(R.styleable.VectorCompatTextView_drawableCompatColor, DEFAULT_COLOR); |
| 116 | + mDrawableCompatTint = ColorStateList.valueOf(color); |
| 117 | + } |
107 | 118 | isDrawableAdjustTextWidth = a.getBoolean(R.styleable.VectorCompatTextView_drawableAdjustTextWidth, false); |
108 | 119 | isDrawableAdjustTextHeight = a.getBoolean(R.styleable.VectorCompatTextView_drawableAdjustTextHeight, false); |
109 | 120 | isDrawableAdjustViewWidth = a.getBoolean(R.styleable.VectorCompatTextView_drawableAdjustViewWidth, false); |
@@ -192,10 +203,11 @@ public void onGlobalLayout() { |
192 | 203 |
|
193 | 204 | private void tintDrawable(Drawable drawable) { |
194 | 205 | if (drawable != null) { |
| 206 | + Drawable wrapped = DrawableCompat.wrap(drawable.mutate()); |
195 | 207 | if (isTintDrawableInTextColor) { |
196 | | - DrawableCompat.setTint(drawable.mutate(), getCurrentTextColor()); |
197 | | - } else if (mDrawableCompatColor >= 0) { |
198 | | - DrawableCompat.setTint(drawable.mutate(), mDrawableCompatColor); |
| 208 | + DrawableCompat.setTint(wrapped, getCurrentTextColor()); |
| 209 | + } else if (mDrawableCompatTint != null) { |
| 210 | + DrawableCompat.setTintList(wrapped, mDrawableCompatTint); |
199 | 211 | } |
200 | 212 | } |
201 | 213 | } |
@@ -392,15 +404,28 @@ public void setTintDrawableInTextColor(boolean tintDrawableInTextColor) { |
392 | 404 | tintCompoundDrawables(); |
393 | 405 | } |
394 | 406 |
|
395 | | - public int getDrawableCompatColor() { |
396 | | - return mDrawableCompatColor; |
| 407 | + public ColorStateList getDrawableCompatTint() { |
| 408 | + return mDrawableCompatTint; |
397 | 409 | } |
398 | 410 |
|
399 | | - public void setDrawableCompatColor(@ColorInt int drawableCompatColor) { |
400 | | - if (mDrawableCompatColor == drawableCompatColor) |
| 411 | + public void setDrawableCompatTint(ColorStateList colorStateList) { |
| 412 | + if (mDrawableCompatTint == colorStateList) |
401 | 413 | return; |
402 | 414 |
|
403 | | - mDrawableCompatColor = drawableCompatColor; |
| 415 | + mDrawableCompatTint = colorStateList; |
| 416 | + tintCompoundDrawables(); |
| 417 | + } |
| 418 | + |
| 419 | + public int getDrawableCompatColor() { |
| 420 | + return mDrawableCompatTint == null ? DEFAULT_COLOR : mDrawableCompatTint.getColorForState(getDrawableState(), DEFAULT_COLOR); |
| 421 | + } |
| 422 | + |
| 423 | + /** |
| 424 | + * @deprecated Use {@link #setDrawableCompatTint} instead. |
| 425 | + */ |
| 426 | + @Deprecated |
| 427 | + public void setDrawableCompatColor(@ColorInt int color) { |
| 428 | + mDrawableCompatTint = ColorStateList.valueOf(color); |
404 | 429 | tintCompoundDrawables(); |
405 | 430 | } |
406 | 431 |
|
@@ -434,7 +459,7 @@ public void toggle() { |
434 | 459 | protected void drawableStateChanged() { |
435 | 460 | super.drawableStateChanged(); |
436 | 461 |
|
437 | | - if (isTintDrawableInTextColor || mDrawableCompatColor >= 0) { |
| 462 | + if (isTintDrawableInTextColor || mDrawableCompatTint != null) { |
438 | 463 | Drawable[] drawables = getCompoundDrawablesInCompatibility(); |
439 | 464 |
|
440 | 465 | boolean needRefresh = false; |
@@ -558,8 +583,17 @@ public CompoundDrawableConfigBuilder tintDrawableInTextColor() { |
558 | 583 | return this; |
559 | 584 | } |
560 | 585 |
|
| 586 | + public CompoundDrawableConfigBuilder setDrawableTint(ColorStateList colorStateList) { |
| 587 | + mVectorCompatTextView.mDrawableCompatTint = colorStateList; |
| 588 | + return this; |
| 589 | + } |
| 590 | + |
| 591 | + /** |
| 592 | + * @deprecated Use {@link #setDrawableTint} instead. |
| 593 | + */ |
| 594 | + @Deprecated |
561 | 595 | public CompoundDrawableConfigBuilder setDrawableColor(@ColorInt int color) { |
562 | | - mVectorCompatTextView.mDrawableCompatColor = color; |
| 596 | + mVectorCompatTextView.mDrawableCompatTint = ColorStateList.valueOf(color); |
563 | 597 | return this; |
564 | 598 | } |
565 | 599 |
|
|
0 commit comments