Skip to content

Commit

Permalink
Supported ColorStateList to tint CompoundDrawables.
Browse files Browse the repository at this point in the history
  • Loading branch information
woxingxiao committed Dec 23, 2020
1 parent 8c80b86 commit 3bbe8f4
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 20 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ android {
applicationId "com.xw.sample.vectorcompattextview"
minSdkVersion 14
targetSdkVersion 28
versionCode 5
versionName "2.3"
versionCode 6
versionName "2.4"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
vectorDrawables.useSupportLibrary = true
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,18 @@
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.app.AppCompatDelegate;
import android.view.View;
import android.widget.RadioGroup;

import com.xw.repo.VectorCompatTextView;

public class MainActivity extends AppCompatActivity {

static {
AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Expand Down
5 changes: 2 additions & 3 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -316,9 +316,8 @@
android:drawablePadding="8dp"
android:gravity="center"
android:text="Day Mode"
android:textColor="@drawable/selector_text_color_day_night_mode"
app:drawableEndCompat="@drawable/selector_drawable_day_night_mode"
app:tintDrawableInTextColor="true"/>
app:drawableCompatTint="@drawable/selector_text_color_day_night_mode"
app:drawableEndCompat="@drawable/selector_drawable_day_night_mode" />
</LinearLayout>

<TextView
Expand Down
4 changes: 2 additions & 2 deletions vectorcompattextview/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ android {
defaultConfig {
minSdkVersion 9
targetSdkVersion 28
versionCode 16
versionName "2.8"
versionCode 17
versionName "2.9"

}
buildTypes {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package com.xw.repo;

import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ColorFilter;
import android.graphics.Paint;
import android.graphics.PixelFormat;
Expand Down Expand Up @@ -36,8 +38,10 @@
*/
public class VectorCompatTextView extends AppCompatCheckedTextView {

private static final int DEFAULT_COLOR = Color.BLACK;

private boolean isTintDrawableInTextColor;
private int mDrawableCompatColor;
private ColorStateList mDrawableCompatTint;
private boolean isDrawableAdjustTextWidth;
private boolean isDrawableAdjustTextHeight;
private boolean isDrawableAdjustViewWidth;
Expand Down Expand Up @@ -103,7 +107,14 @@ private void initAttrs(Context context, AttributeSet attrs) {
}

isTintDrawableInTextColor = a.getBoolean(R.styleable.VectorCompatTextView_tintDrawableInTextColor, false);
mDrawableCompatColor = a.getColor(R.styleable.VectorCompatTextView_drawableCompatColor, -1);
if (a.hasValue(R.styleable.VectorCompatTextView_drawableCompatTint)) {
mDrawableCompatTint = a.getColorStateList(R.styleable.VectorCompatTextView_drawableCompatTint);
} else if (a.hasValue(R.styleable.VectorCompatTextView_drawableCompatColor)) {
// @deprecated
// Use drawableCompatTint instead.
int color = a.getColor(R.styleable.VectorCompatTextView_drawableCompatColor, DEFAULT_COLOR);
mDrawableCompatTint = ColorStateList.valueOf(color);
}
isDrawableAdjustTextWidth = a.getBoolean(R.styleable.VectorCompatTextView_drawableAdjustTextWidth, false);
isDrawableAdjustTextHeight = a.getBoolean(R.styleable.VectorCompatTextView_drawableAdjustTextHeight, false);
isDrawableAdjustViewWidth = a.getBoolean(R.styleable.VectorCompatTextView_drawableAdjustViewWidth, false);
Expand Down Expand Up @@ -192,10 +203,11 @@ public void onGlobalLayout() {

private void tintDrawable(Drawable drawable) {
if (drawable != null) {
Drawable wrapped = DrawableCompat.wrap(drawable.mutate());
if (isTintDrawableInTextColor) {
DrawableCompat.setTint(drawable.mutate(), getCurrentTextColor());
} else if (mDrawableCompatColor >= 0) {
DrawableCompat.setTint(drawable.mutate(), mDrawableCompatColor);
DrawableCompat.setTint(wrapped, getCurrentTextColor());
} else if (mDrawableCompatTint != null) {
DrawableCompat.setTintList(wrapped, mDrawableCompatTint);
}
}
}
Expand Down Expand Up @@ -392,15 +404,28 @@ public void setTintDrawableInTextColor(boolean tintDrawableInTextColor) {
tintCompoundDrawables();
}

public int getDrawableCompatColor() {
return mDrawableCompatColor;
public ColorStateList getDrawableCompatTint() {
return mDrawableCompatTint;
}

public void setDrawableCompatColor(@ColorInt int drawableCompatColor) {
if (mDrawableCompatColor == drawableCompatColor)
public void setDrawableCompatTint(ColorStateList colorStateList) {
if (mDrawableCompatTint == colorStateList)
return;

mDrawableCompatColor = drawableCompatColor;
mDrawableCompatTint = colorStateList;
tintCompoundDrawables();
}

public int getDrawableCompatColor() {
return mDrawableCompatTint == null ? DEFAULT_COLOR : mDrawableCompatTint.getColorForState(getDrawableState(), DEFAULT_COLOR);
}

/**
* @deprecated Use {@link #setDrawableCompatTint} instead.
*/
@Deprecated
public void setDrawableCompatColor(@ColorInt int color) {
mDrawableCompatTint = ColorStateList.valueOf(color);
tintCompoundDrawables();
}

Expand Down Expand Up @@ -434,7 +459,7 @@ public void toggle() {
protected void drawableStateChanged() {
super.drawableStateChanged();

if (isTintDrawableInTextColor || mDrawableCompatColor >= 0) {
if (isTintDrawableInTextColor || mDrawableCompatTint != null) {
Drawable[] drawables = getCompoundDrawablesInCompatibility();

boolean needRefresh = false;
Expand Down Expand Up @@ -558,8 +583,17 @@ public CompoundDrawableConfigBuilder tintDrawableInTextColor() {
return this;
}

public CompoundDrawableConfigBuilder setDrawableTint(ColorStateList colorStateList) {
mVectorCompatTextView.mDrawableCompatTint = colorStateList;
return this;
}

/**
* @deprecated Use {@link #setDrawableTint} instead.
*/
@Deprecated
public CompoundDrawableConfigBuilder setDrawableColor(@ColorInt int color) {
mVectorCompatTextView.mDrawableCompatColor = color;
mVectorCompatTextView.mDrawableCompatTint = ColorStateList.valueOf(color);
return this;
}

Expand Down
3 changes: 2 additions & 1 deletion vectorcompattextview/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="VectorCompatTextView">
<attr name="drawableStartCompat" format="reference"/>
Expand All @@ -8,7 +7,9 @@
<attr name="drawableTopCompat" format="reference"/>
<attr name="drawableBottomCompat" format="reference"/>
<attr name="tintDrawableInTextColor" format="boolean"/>
<!-- @deprecated Use drawableCompatTint instead. -->
<attr name="drawableCompatColor" format="color|reference"/>
<attr name="drawableCompatTint" format="color|reference"/>
<attr name="drawableAdjustTextWidth" format="boolean"/>
<attr name="drawableAdjustTextHeight" format="boolean"/>
<attr name="drawableAdjustViewWidth" format="boolean"/>
Expand Down

0 comments on commit 3bbe8f4

Please sign in to comment.