From dd41a0a4984486b6761142da10169694c06b6849 Mon Sep 17 00:00:00 2001 From: Mariotaku Lee Date: Thu, 22 Dec 2016 14:04:49 +0800 Subject: [PATCH] [NO-CI] improved TextView theme --- .../view/ChameleonAutoCompleteTextView.java | 4 +- .../chameleon/view/ChameleonEditText.java | 65 +------------- .../ChameleonMultiAutoCompleteTextView.java | 4 +- .../chameleon/view/ChameleonTextView.java | 87 +++++++++++++++++++ 4 files changed, 93 insertions(+), 67 deletions(-) diff --git a/chameleon/src/main/java/org/mariotaku/chameleon/view/ChameleonAutoCompleteTextView.java b/chameleon/src/main/java/org/mariotaku/chameleon/view/ChameleonAutoCompleteTextView.java index cbd883372..c72e23190 100644 --- a/chameleon/src/main/java/org/mariotaku/chameleon/view/ChameleonAutoCompleteTextView.java +++ b/chameleon/src/main/java/org/mariotaku/chameleon/view/ChameleonAutoCompleteTextView.java @@ -34,14 +34,14 @@ public class ChameleonAutoCompleteTextView extends AppCompatAutoCompleteTextView @Nullable @Override public ChameleonEditText.Appearance createAppearance(@NonNull Context context, @NonNull AttributeSet attributeSet, @NonNull Chameleon.Theme theme) { - return ChameleonEditText.Appearance.create(context, attributeSet, theme); + return ChameleonTextView.Appearance.create(context, attributeSet, theme); } @Override public void applyAppearance(@NonNull Appearance appearance) { final ChameleonEditText.Appearance a = (ChameleonEditText.Appearance) appearance; - ChameleonEditText.Appearance.apply(this, a); + ChameleonTextView.Appearance.apply(this, a); } } diff --git a/chameleon/src/main/java/org/mariotaku/chameleon/view/ChameleonEditText.java b/chameleon/src/main/java/org/mariotaku/chameleon/view/ChameleonEditText.java index f856ab202..c30bea502 100644 --- a/chameleon/src/main/java/org/mariotaku/chameleon/view/ChameleonEditText.java +++ b/chameleon/src/main/java/org/mariotaku/chameleon/view/ChameleonEditText.java @@ -1,24 +1,13 @@ package org.mariotaku.chameleon.view; import android.content.Context; -import android.content.res.ColorStateList; -import android.graphics.drawable.Drawable; -import android.support.annotation.ColorInt; import android.support.annotation.NonNull; import android.support.annotation.Nullable; -import android.support.v4.content.ContextCompat; -import android.support.v4.view.ViewCompat; import android.support.v7.widget.AppCompatEditText; import android.util.AttributeSet; -import android.widget.TextView; import org.mariotaku.chameleon.Chameleon; -import org.mariotaku.chameleon.ChameleonUtils; import org.mariotaku.chameleon.ChameleonView; -import org.mariotaku.chameleon.R; -import org.mariotaku.chameleon.internal.ChameleonTypedArray; - -import java.lang.reflect.Field; /** * Created by mariotaku on 2016/12/18. @@ -45,64 +34,14 @@ public class ChameleonEditText extends AppCompatEditText implements ChameleonVie @Nullable @Override public Appearance createAppearance(@NonNull Context context, @NonNull AttributeSet attributeSet, @NonNull Chameleon.Theme theme) { - return Appearance.create(context, attributeSet, theme); + return ChameleonTextView.Appearance.create(context, attributeSet, theme); } @Override public void applyAppearance(@NonNull ChameleonView.Appearance appearance) { final Appearance a = (Appearance) appearance; - Appearance.apply(this, a); + ChameleonTextView.Appearance.apply(this, a); } - public static class Appearance extends ChameleonTextView.Appearance { - private int backgroundColor; - - public int getBackgroundColor() { - return backgroundColor; - } - - public void setBackgroundColor(int backgroundColor) { - this.backgroundColor = backgroundColor; - } - - public static void apply(TextView view, Appearance appearance) { - view.setLinkTextColor(appearance.getLinkTextColor()); - ViewCompat.setBackgroundTintList(view, ColorStateList.valueOf(appearance.getBackgroundColor())); - setCursorTint(view, appearance.getBackgroundColor()); - } - - public static Appearance create(Context context, AttributeSet attributeSet, Chameleon.Theme theme) { - Appearance appearance = new Appearance(); - ChameleonTypedArray a = ChameleonTypedArray.obtain(context, attributeSet, - R.styleable.ChameleonEditText, theme); - appearance.setLinkTextColor(a.getColor(R.styleable.ChameleonEditText_android_textColorLink, theme.getTextColorLink())); - appearance.setBackgroundColor(a.getColor(R.styleable.ChameleonEditText_backgroundTint, theme.getColorAccent())); - a.recycle(); - return appearance; - } - - - public static void setCursorTint(@NonNull TextView textView, @ColorInt int color) { - try { - Field fCursorDrawableRes = TextView.class.getDeclaredField("mCursorDrawableRes"); - fCursorDrawableRes.setAccessible(true); - int mCursorDrawableRes = fCursorDrawableRes.getInt(textView); - Field fEditor = TextView.class.getDeclaredField("mEditor"); - fEditor.setAccessible(true); - Object editor = fEditor.get(textView); - Class clazz = editor.getClass(); - Field fCursorDrawable = clazz.getDeclaredField("mCursorDrawable"); - fCursorDrawable.setAccessible(true); - Drawable[] drawables = new Drawable[2]; - drawables[0] = ContextCompat.getDrawable(textView.getContext(), mCursorDrawableRes); - drawables[0] = ChameleonUtils.createTintedDrawable(drawables[0], color); - drawables[1] = ContextCompat.getDrawable(textView.getContext(), mCursorDrawableRes); - drawables[1] = ChameleonUtils.createTintedDrawable(drawables[1], color); - fCursorDrawable.set(editor, drawables); - } catch (Exception e) { - e.printStackTrace(); - } - } - } } diff --git a/chameleon/src/main/java/org/mariotaku/chameleon/view/ChameleonMultiAutoCompleteTextView.java b/chameleon/src/main/java/org/mariotaku/chameleon/view/ChameleonMultiAutoCompleteTextView.java index a1142aa11..0088e14a6 100644 --- a/chameleon/src/main/java/org/mariotaku/chameleon/view/ChameleonMultiAutoCompleteTextView.java +++ b/chameleon/src/main/java/org/mariotaku/chameleon/view/ChameleonMultiAutoCompleteTextView.java @@ -34,14 +34,14 @@ public class ChameleonMultiAutoCompleteTextView extends AppCompatMultiAutoComple @Nullable @Override public ChameleonEditText.Appearance createAppearance(@NonNull Context context, @NonNull AttributeSet attributeSet, @NonNull Chameleon.Theme theme) { - return ChameleonEditText.Appearance.create(context, attributeSet, theme); + return ChameleonTextView.Appearance.create(context, attributeSet, theme); } @Override public void applyAppearance(@NonNull ChameleonView.Appearance appearance) { final ChameleonEditText.Appearance a = (ChameleonEditText.Appearance) appearance; - ChameleonEditText.Appearance.apply(this, a); + ChameleonTextView.Appearance.apply(this, a); } } diff --git a/chameleon/src/main/java/org/mariotaku/chameleon/view/ChameleonTextView.java b/chameleon/src/main/java/org/mariotaku/chameleon/view/ChameleonTextView.java index 9c10996d0..b14e8d80f 100644 --- a/chameleon/src/main/java/org/mariotaku/chameleon/view/ChameleonTextView.java +++ b/chameleon/src/main/java/org/mariotaku/chameleon/view/ChameleonTextView.java @@ -1,16 +1,25 @@ package org.mariotaku.chameleon.view; import android.content.Context; +import android.content.res.ColorStateList; +import android.graphics.drawable.Drawable; +import android.support.annotation.ColorInt; import android.support.annotation.NonNull; import android.support.annotation.Nullable; +import android.support.v4.content.ContextCompat; +import android.support.v4.view.ViewCompat; import android.support.v7.widget.AppCompatTextView; import android.util.AttributeSet; +import android.widget.TextView; import org.mariotaku.chameleon.Chameleon; +import org.mariotaku.chameleon.ChameleonUtils; import org.mariotaku.chameleon.ChameleonView; import org.mariotaku.chameleon.R; import org.mariotaku.chameleon.internal.ChameleonTypedArray; +import java.lang.reflect.Field; + /** * Created by mariotaku on 2016/12/18. */ @@ -53,6 +62,76 @@ public class ChameleonTextView extends AppCompatTextView implements ChameleonVie public static class Appearance implements ChameleonView.Appearance { private int linkTextColor; + private int backgroundColor; + + public static void apply(TextView view, ChameleonEditText.Appearance appearance) { + view.setLinkTextColor(appearance.getLinkTextColor()); + ViewCompat.setBackgroundTintList(view, ColorStateList.valueOf(appearance.getBackgroundColor())); + setCursorTint(view, appearance.getBackgroundColor()); + setHandlerTint(view, appearance.getBackgroundColor()); + view.setHighlightColor(ChameleonUtils.adjustAlpha(appearance.getBackgroundColor(), 0.4f)); + } + + public static ChameleonEditText.Appearance create(Context context, AttributeSet attributeSet, Chameleon.Theme theme) { + ChameleonEditText.Appearance appearance = new ChameleonEditText.Appearance(); + ChameleonTypedArray a = ChameleonTypedArray.obtain(context, attributeSet, + R.styleable.ChameleonEditText, theme); + appearance.setLinkTextColor(a.getColor(R.styleable.ChameleonEditText_android_textColorLink, theme.getTextColorLink())); + appearance.setBackgroundColor(a.getColor(R.styleable.ChameleonEditText_backgroundTint, theme.getColorAccent())); + a.recycle(); + return appearance; + } + + public static void setCursorTint(@NonNull TextView textView, @ColorInt int color) { + try { + int mCursorDrawableRes = getIntField(TextView.class, textView, "mCursorDrawableRes"); + Object editor = getField(TextView.class, textView, "mEditor"); + Drawable[] drawables = new Drawable[2]; + drawables[0] = ContextCompat.getDrawable(textView.getContext(), mCursorDrawableRes); + drawables[0] = ChameleonUtils.createTintedDrawable(drawables[0], color); + drawables[1] = ContextCompat.getDrawable(textView.getContext(), mCursorDrawableRes); + drawables[1] = ChameleonUtils.createTintedDrawable(drawables[1], color); + setField(Class.forName("android.widget.Editor"), editor, "mCursorDrawable", drawables); + } catch (Exception e) { + // Ignore + e.printStackTrace(); + } + } + + public static void setHandlerTint(@NonNull TextView textView, @ColorInt int color) { + try { + int mTextSelectHandleLeftRes = getIntField(TextView.class, textView, "mTextSelectHandleLeftRes"); + int mTextSelectHandleRightRes = getIntField(TextView.class, textView, "mTextSelectHandleRightRes"); + int mTextSelectHandleRes = getIntField(TextView.class, textView, "mTextSelectHandleRes"); + Object editor = getField(TextView.class, textView, "mEditor"); + final Class editorClass = Class.forName("android.widget.Editor"); + setField(editorClass, editor, "mSelectHandleLeft", ChameleonUtils.createTintedDrawable(ContextCompat.getDrawable(textView.getContext(), mTextSelectHandleLeftRes), color)); + setField(Class.forName("android.widget.Editor"), editor, "mSelectHandleRight", ChameleonUtils.createTintedDrawable(ContextCompat.getDrawable(textView.getContext(), mTextSelectHandleRightRes), color)); + setField(Class.forName("android.widget.Editor"), editor, "mSelectHandleCenter", ChameleonUtils.createTintedDrawable(ContextCompat.getDrawable(textView.getContext(), mTextSelectHandleRes), color)); + } catch (Exception e) { + // Ignore + e.printStackTrace(); + } + + } + + private static Object getField(@NonNull Class cls, @NonNull Object object, String fieldName) throws NoSuchFieldException, IllegalAccessException { + Field field = cls.getDeclaredField(fieldName); + field.setAccessible(true); + return field.get(object); + } + + private static int getIntField(@NonNull Class cls, @NonNull Object object, String fieldName) throws NoSuchFieldException, IllegalAccessException { + Field field = cls.getDeclaredField(fieldName); + field.setAccessible(true); + return field.getInt(object); + } + + private static void setField(@NonNull Class cls, @NonNull Object object, String fieldName, Object value) throws NoSuchFieldException, IllegalAccessException { + Field field = cls.getDeclaredField(fieldName); + field.setAccessible(true); + field.set(object, value); + } public int getLinkTextColor() { return linkTextColor; @@ -61,5 +140,13 @@ public class ChameleonTextView extends AppCompatTextView implements ChameleonVie public void setLinkTextColor(int linkTextColor) { this.linkTextColor = linkTextColor; } + + public int getBackgroundColor() { + return backgroundColor; + } + + public void setBackgroundColor(int backgroundColor) { + this.backgroundColor = backgroundColor; + } } }