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 5867f13 commit 3547457
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 23 deletions.
12 changes: 6 additions & 6 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ android {
applicationId "com.xw.sample.vectorcompattextview"
minSdkVersion 14
targetSdkVersion 29
versionCode 4
versionName "2.2"
versionCode 5
versionName "2.3"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
vectorDrawables.useSupportLibrary = true
}
Expand All @@ -23,10 +23,10 @@ android {

dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
testImplementation 'junit:junit:4.13.1'
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation project(':vectorcompattextview')
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,17 @@ package com.xw.sample.vectorcompattextview

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.app.AppCompatDelegate
import androidx.core.content.ContextCompat
import com.xw.repo.VectorCompatTextView.CompoundDrawableConfigBuilder
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {

init {
AppCompatDelegate.setCompatVectorFromResourcesEnabled(true)
}

override fun onCreate(savedInstanceState: Bundle?) {
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 29
versionCode 16
versionName "2.8-androidx"
versionCode 17
versionName "2.9-androidx"

}
buildTypes {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
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;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.os.Build;

import androidx.annotation.ColorInt;
import androidx.annotation.DrawableRes;
import androidx.annotation.NonNull;
Expand All @@ -18,6 +21,7 @@
import androidx.core.graphics.drawable.DrawableCompat;
import androidx.appcompat.content.res.AppCompatResources;
import androidx.appcompat.widget.AppCompatCheckedTextView;

import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.ViewTreeObserver;
Expand All @@ -36,8 +40,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 +109,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 +205,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 +406,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 +461,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 +585,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
2 changes: 2 additions & 0 deletions vectorcompattextview/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,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 3547457

Please sign in to comment.