Skip to content

Commit 3bbe8f4

Browse files
committed
Supported ColorStateList to tint CompoundDrawables.
1 parent 8c80b86 commit 3bbe8f4

File tree

6 files changed

+59
-20
lines changed

6 files changed

+59
-20
lines changed

app/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ android {
66
applicationId "com.xw.sample.vectorcompattextview"
77
minSdkVersion 14
88
targetSdkVersion 28
9-
versionCode 5
10-
versionName "2.3"
9+
versionCode 6
10+
versionName "2.4"
1111
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
1212
vectorDrawables.useSupportLibrary = true
1313
}

app/src/main/java/com/xw/sample/vectorcompattextview/MainActivity.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,18 @@
33
import android.os.Bundle;
44
import android.support.v4.content.ContextCompat;
55
import android.support.v7.app.AppCompatActivity;
6+
import android.support.v7.app.AppCompatDelegate;
67
import android.view.View;
78
import android.widget.RadioGroup;
89

910
import com.xw.repo.VectorCompatTextView;
1011

1112
public class MainActivity extends AppCompatActivity {
1213

14+
static {
15+
AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
16+
}
17+
1318
@Override
1419
protected void onCreate(Bundle savedInstanceState) {
1520
super.onCreate(savedInstanceState);

app/src/main/res/layout/activity_main.xml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -316,9 +316,8 @@
316316
android:drawablePadding="8dp"
317317
android:gravity="center"
318318
android:text="Day Mode"
319-
android:textColor="@drawable/selector_text_color_day_night_mode"
320-
app:drawableEndCompat="@drawable/selector_drawable_day_night_mode"
321-
app:tintDrawableInTextColor="true"/>
319+
app:drawableCompatTint="@drawable/selector_text_color_day_night_mode"
320+
app:drawableEndCompat="@drawable/selector_drawable_day_night_mode" />
322321
</LinearLayout>
323322

324323
<TextView

vectorcompattextview/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ android {
66
defaultConfig {
77
minSdkVersion 9
88
targetSdkVersion 28
9-
versionCode 16
10-
versionName "2.8"
9+
versionCode 17
10+
versionName "2.9"
1111

1212
}
1313
buildTypes {

vectorcompattextview/src/main/java/com/xw/repo/VectorCompatTextView.java

Lines changed: 46 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package com.xw.repo;
22

33
import android.content.Context;
4+
import android.content.res.ColorStateList;
45
import android.content.res.Resources;
56
import android.content.res.TypedArray;
67
import android.graphics.Canvas;
8+
import android.graphics.Color;
79
import android.graphics.ColorFilter;
810
import android.graphics.Paint;
911
import android.graphics.PixelFormat;
@@ -36,8 +38,10 @@
3638
*/
3739
public class VectorCompatTextView extends AppCompatCheckedTextView {
3840

41+
private static final int DEFAULT_COLOR = Color.BLACK;
42+
3943
private boolean isTintDrawableInTextColor;
40-
private int mDrawableCompatColor;
44+
private ColorStateList mDrawableCompatTint;
4145
private boolean isDrawableAdjustTextWidth;
4246
private boolean isDrawableAdjustTextHeight;
4347
private boolean isDrawableAdjustViewWidth;
@@ -103,7 +107,14 @@ private void initAttrs(Context context, AttributeSet attrs) {
103107
}
104108

105109
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+
}
107118
isDrawableAdjustTextWidth = a.getBoolean(R.styleable.VectorCompatTextView_drawableAdjustTextWidth, false);
108119
isDrawableAdjustTextHeight = a.getBoolean(R.styleable.VectorCompatTextView_drawableAdjustTextHeight, false);
109120
isDrawableAdjustViewWidth = a.getBoolean(R.styleable.VectorCompatTextView_drawableAdjustViewWidth, false);
@@ -192,10 +203,11 @@ public void onGlobalLayout() {
192203

193204
private void tintDrawable(Drawable drawable) {
194205
if (drawable != null) {
206+
Drawable wrapped = DrawableCompat.wrap(drawable.mutate());
195207
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);
199211
}
200212
}
201213
}
@@ -392,15 +404,28 @@ public void setTintDrawableInTextColor(boolean tintDrawableInTextColor) {
392404
tintCompoundDrawables();
393405
}
394406

395-
public int getDrawableCompatColor() {
396-
return mDrawableCompatColor;
407+
public ColorStateList getDrawableCompatTint() {
408+
return mDrawableCompatTint;
397409
}
398410

399-
public void setDrawableCompatColor(@ColorInt int drawableCompatColor) {
400-
if (mDrawableCompatColor == drawableCompatColor)
411+
public void setDrawableCompatTint(ColorStateList colorStateList) {
412+
if (mDrawableCompatTint == colorStateList)
401413
return;
402414

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);
404429
tintCompoundDrawables();
405430
}
406431

@@ -434,7 +459,7 @@ public void toggle() {
434459
protected void drawableStateChanged() {
435460
super.drawableStateChanged();
436461

437-
if (isTintDrawableInTextColor || mDrawableCompatColor >= 0) {
462+
if (isTintDrawableInTextColor || mDrawableCompatTint != null) {
438463
Drawable[] drawables = getCompoundDrawablesInCompatibility();
439464

440465
boolean needRefresh = false;
@@ -558,8 +583,17 @@ public CompoundDrawableConfigBuilder tintDrawableInTextColor() {
558583
return this;
559584
}
560585

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
561595
public CompoundDrawableConfigBuilder setDrawableColor(@ColorInt int color) {
562-
mVectorCompatTextView.mDrawableCompatColor = color;
596+
mVectorCompatTextView.mDrawableCompatTint = ColorStateList.valueOf(color);
563597
return this;
564598
}
565599

vectorcompattextview/src/main/res/values/attrs.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
<?xml version="1.0" encoding="utf-8"?>
21
<resources>
32
<declare-styleable name="VectorCompatTextView">
43
<attr name="drawableStartCompat" format="reference"/>
@@ -8,7 +7,9 @@
87
<attr name="drawableTopCompat" format="reference"/>
98
<attr name="drawableBottomCompat" format="reference"/>
109
<attr name="tintDrawableInTextColor" format="boolean"/>
10+
<!-- @deprecated Use drawableCompatTint instead. -->
1111
<attr name="drawableCompatColor" format="color|reference"/>
12+
<attr name="drawableCompatTint" format="color|reference"/>
1213
<attr name="drawableAdjustTextWidth" format="boolean"/>
1314
<attr name="drawableAdjustTextHeight" format="boolean"/>
1415
<attr name="drawableAdjustViewWidth" format="boolean"/>

0 commit comments

Comments
 (0)