diff --git a/app/build.gradle b/app/build.gradle index bcf3682..0eb5f60 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -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 } diff --git a/app/src/main/java/com/xw/sample/vectorcompattextview/MainActivity.java b/app/src/main/java/com/xw/sample/vectorcompattextview/MainActivity.java index 96faba6..3ffd41e 100644 --- a/app/src/main/java/com/xw/sample/vectorcompattextview/MainActivity.java +++ b/app/src/main/java/com/xw/sample/vectorcompattextview/MainActivity.java @@ -3,6 +3,7 @@ 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; @@ -10,6 +11,10 @@ public class MainActivity extends AppCompatActivity { + static { + AppCompatDelegate.setCompatVectorFromResourcesEnabled(true); + } + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 446657f..952b58a 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -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" /> = 0) { - DrawableCompat.setTint(drawable.mutate(), mDrawableCompatColor); + DrawableCompat.setTint(wrapped, getCurrentTextColor()); + } else if (mDrawableCompatTint != null) { + DrawableCompat.setTintList(wrapped, mDrawableCompatTint); } } } @@ -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(); } @@ -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; @@ -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; } diff --git a/vectorcompattextview/src/main/res/values/attrs.xml b/vectorcompattextview/src/main/res/values/attrs.xml index c702e35..cdd67e8 100644 --- a/vectorcompattextview/src/main/res/values/attrs.xml +++ b/vectorcompattextview/src/main/res/values/attrs.xml @@ -1,4 +1,3 @@ - @@ -8,7 +7,9 @@ + +