From a80ca4c7e09fc387a7dd38d61326c0b66cd4da22 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=8F=E8=B5=9F=E4=B8=9E?= Date: Mon, 6 Nov 2017 15:24:26 +0800 Subject: [PATCH] obtain attributeset values without hard coding --- .../widgets/AppCompatBackgroundHelper.java | 18 ++--- .../AppCompatCompoundButtonHelper.java | 17 ++--- .../AppCompatCompoundDrawableHelper.java | 43 ++++++------ .../widgets/AppCompatForegroundHelper.java | 17 ++--- .../widgets/AppCompatImageHelper.java | 35 ++++------ .../widgets/AppCompatProgressBarHelper.java | 18 ++--- .../widgets/AppCompatTextHelper.java | 15 ++-- .../widgets/TintSwitchCompat.java | 4 +- magicasakura/src/main/res/values/attrs.xml | 70 ++++++++++++++++--- 9 files changed, 126 insertions(+), 111 deletions(-) diff --git a/magicasakura/src/main/java/com/bilibili/magicasakura/widgets/AppCompatBackgroundHelper.java b/magicasakura/src/main/java/com/bilibili/magicasakura/widgets/AppCompatBackgroundHelper.java index a98dc9a..b47c21f 100644 --- a/magicasakura/src/main/java/com/bilibili/magicasakura/widgets/AppCompatBackgroundHelper.java +++ b/magicasakura/src/main/java/com/bilibili/magicasakura/widgets/AppCompatBackgroundHelper.java @@ -37,12 +37,6 @@ */ public class AppCompatBackgroundHelper extends AppCompatBaseHelper { - private static final int[] ATTR = { - android.R.attr.background, - R.attr.backgroundTint, - R.attr.backgroundTintMode - }; - private TintInfo mBackgroundTintInfo; private int mBackgroundResId; @@ -58,15 +52,15 @@ public AppCompatBackgroundHelper(View view, TintManager tintManager) { @Override void loadFromAttribute(AttributeSet attrs, int defStyleAttr) { initPadding(); - TypedArray array = mView.getContext().obtainStyledAttributes(attrs, ATTR, defStyleAttr, 0); - if (array.hasValue(1)) { - mBackgroundTintResId = array.getResourceId(1, 0); - if (array.hasValue(2)) { - setSupportBackgroundTintMode(DrawableUtils.parseTintMode(array.getInt(2, 0), null)); + TypedArray array = mView.getContext().obtainStyledAttributes(attrs, R.styleable.TintViewBackgroundHelper, defStyleAttr, 0); + if (array.hasValue(R.styleable.TintViewBackgroundHelper_backgroundTint)) { + mBackgroundTintResId = array.getResourceId(R.styleable.TintViewBackgroundHelper_backgroundTint, 0); + if (array.hasValue(R.styleable.TintViewBackgroundHelper_backgroundTintMode)) { + setSupportBackgroundTintMode(DrawableUtils.parseTintMode(array.getInt(R.styleable.TintViewBackgroundHelper_backgroundTintMode, 0), null)); } setSupportBackgroundTint(mBackgroundTintResId); } else { - Drawable drawable = mTintManager.getDrawable(mBackgroundResId = array.getResourceId(0, 0)); + Drawable drawable = mTintManager.getDrawable(mBackgroundResId = array.getResourceId(R.styleable.TintViewBackgroundHelper_android_background, 0)); if (drawable != null) { setBackgroundDrawable(drawable); } diff --git a/magicasakura/src/main/java/com/bilibili/magicasakura/widgets/AppCompatCompoundButtonHelper.java b/magicasakura/src/main/java/com/bilibili/magicasakura/widgets/AppCompatCompoundButtonHelper.java index 34c4907..48b3362 100644 --- a/magicasakura/src/main/java/com/bilibili/magicasakura/widgets/AppCompatCompoundButtonHelper.java +++ b/magicasakura/src/main/java/com/bilibili/magicasakura/widgets/AppCompatCompoundButtonHelper.java @@ -36,11 +36,6 @@ * @time 15/11/23 */ public class AppCompatCompoundButtonHelper extends AppCompatBaseHelper { - private static final int[] ATTRS = { - android.R.attr.button, - R.attr.compoundButtonTint, - R.attr.compoundButtonTintMode - }; private TintInfo mCompoundButtonTintInfo; private int mCompoundButtonResId; @@ -53,15 +48,15 @@ public AppCompatCompoundButtonHelper(CompoundButton view, TintManager tintManage @SuppressWarnings("ResourceType") @Override void loadFromAttribute(AttributeSet attrs, int defStyleAttr) { - TypedArray array = mView.getContext().obtainStyledAttributes(attrs, ATTRS, defStyleAttr, 0); - if (array.hasValue(1)) { - mCompoundButtonTintResId = array.getResourceId(1, 0); - if (array.hasValue(2)) { - setSupportButtonDrawableTintMode(DrawableUtils.parseTintMode(array.getInt(2, 0), null)); + TypedArray array = mView.getContext().obtainStyledAttributes(attrs, R.styleable.TintCompoundButtonHelper, defStyleAttr, 0); + if (array.hasValue(R.styleable.TintCompoundButtonHelper_compoundButtonTint)) { + mCompoundButtonTintResId = array.getResourceId(R.styleable.TintCompoundButtonHelper_compoundButtonTint, 0); + if (array.hasValue(R.styleable.TintCompoundButtonHelper_compoundButtonTintMode)) { + setSupportButtonDrawableTintMode(DrawableUtils.parseTintMode(array.getInt(R.styleable.TintCompoundButtonHelper_compoundButtonTintMode, 0), null)); } setSupportButtonDrawableTint(mCompoundButtonTintResId); } else { - Drawable drawable = mTintManager.getDrawable(mCompoundButtonResId = array.getResourceId(0, 0)); + Drawable drawable = mTintManager.getDrawable(mCompoundButtonResId = array.getResourceId(R.styleable.TintCompoundButtonHelper_android_button, 0)); if (drawable != null) { setButtonDrawable(drawable); } diff --git a/magicasakura/src/main/java/com/bilibili/magicasakura/widgets/AppCompatCompoundDrawableHelper.java b/magicasakura/src/main/java/com/bilibili/magicasakura/widgets/AppCompatCompoundDrawableHelper.java index 8ad10a4..7e3018a 100644 --- a/magicasakura/src/main/java/com/bilibili/magicasakura/widgets/AppCompatCompoundDrawableHelper.java +++ b/magicasakura/src/main/java/com/bilibili/magicasakura/widgets/AppCompatCompoundDrawableHelper.java @@ -37,17 +37,6 @@ */ public class AppCompatCompoundDrawableHelper extends AppCompatBaseHelper { - private static final int[] ATTR = { - R.attr.drawableLeftTint, - R.attr.drawableTopTint, - R.attr.drawableRightTint, - R.attr.drawableBottomTint, - R.attr.drawableLeftTintMode, - R.attr.drawableTopTintMode, - R.attr.drawableRightTintMode, - R.attr.drawableBottomTintMode - }; - private TintInfo[] mCompoundDrawableTintInfos = new TintInfo[4]; private int[] mCompoundDrawableResIds = new int[4]; @@ -62,19 +51,27 @@ public AppCompatCompoundDrawableHelper(TextView view, TintManager tintManager) { @Override void loadFromAttribute(AttributeSet attrs, int defStyleAttr) { Context context = mView.getContext(); - TypedArray a = context.obtainStyledAttributes(attrs, ATTR, defStyleAttr, 0); - for (int tintIndex = 0; tintIndex < 4; tintIndex++) { - int modeIndex = tintIndex + 4; - mCompoundDrawableResIds[tintIndex] = a.getResourceId(tintIndex, 0); - mCompoundDrawableTintResIds[tintIndex] = a.getResourceId(tintIndex, 0); - if (a.hasValue(modeIndex)) { - mCompoundDrawableTintModes[tintIndex] = DrawableUtils.parseTintMode(a.getInt(modeIndex, 0), null); - } + TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.TintCompoundDrawableHelper, defStyleAttr, 0); + mCompoundDrawableResIds[0] = a.getResourceId(R.styleable.TintCompoundDrawableHelper_android_drawableLeft, 0); + mCompoundDrawableTintResIds[0] = a.getResourceId(R.styleable.TintCompoundDrawableHelper_drawableLeftTint, 0); + if (a.hasValue(R.styleable.TintCompoundDrawableHelper_drawableLeftTintMode)) { + mCompoundDrawableTintModes[0] = DrawableUtils.parseTintMode(a.getInt(R.styleable.TintCompoundDrawableHelper_drawableLeftTintMode, 0), null); + } + mCompoundDrawableResIds[1] = a.getResourceId(R.styleable.TintCompoundDrawableHelper_android_drawableTop, 0); + mCompoundDrawableTintResIds[1] = a.getResourceId(R.styleable.TintCompoundDrawableHelper_drawableTopTint, 0); + if (a.hasValue(R.styleable.TintCompoundDrawableHelper_drawableTopTintMode)) { + mCompoundDrawableTintModes[1] = DrawableUtils.parseTintMode(a.getInt(R.styleable.TintCompoundDrawableHelper_drawableTopTintMode, 0), null); + } + mCompoundDrawableResIds[2] = a.getResourceId(R.styleable.TintCompoundDrawableHelper_android_drawableRight, 0); + mCompoundDrawableTintResIds[2] = a.getResourceId(R.styleable.TintCompoundDrawableHelper_drawableRightTint, 0); + if (a.hasValue(R.styleable.TintCompoundDrawableHelper_drawableRightTintMode)) { + mCompoundDrawableTintModes[2] = DrawableUtils.parseTintMode(a.getInt(R.styleable.TintCompoundDrawableHelper_drawableRightTintMode, 0), null); + } + mCompoundDrawableResIds[3] = a.getResourceId(R.styleable.TintCompoundDrawableHelper_android_drawableBottom, 0); + mCompoundDrawableTintResIds[3] = a.getResourceId(R.styleable.TintCompoundDrawableHelper_drawableBottomTint, 0); + if (a.hasValue(R.styleable.TintCompoundDrawableHelper_drawableBottomTintMode)) { + mCompoundDrawableTintModes[3] = DrawableUtils.parseTintMode(a.getInt(R.styleable.TintCompoundDrawableHelper_drawableBottomTintMode, 0), null); } - mCompoundDrawableResIds[0] = ThemeUtils.getThemeAttrId(context, attrs, android.R.attr.drawableLeft); - mCompoundDrawableResIds[1] = ThemeUtils.getThemeAttrId(context, attrs, android.R.attr.drawableTop); - mCompoundDrawableResIds[2] = ThemeUtils.getThemeAttrId(context, attrs, android.R.attr.drawableRight); - mCompoundDrawableResIds[3] = ThemeUtils.getThemeAttrId(context, attrs, android.R.attr.drawableBottom); a.recycle(); setCompoundDrawablesWithIntrinsicBounds( diff --git a/magicasakura/src/main/java/com/bilibili/magicasakura/widgets/AppCompatForegroundHelper.java b/magicasakura/src/main/java/com/bilibili/magicasakura/widgets/AppCompatForegroundHelper.java index 0b11425..d1130b1 100644 --- a/magicasakura/src/main/java/com/bilibili/magicasakura/widgets/AppCompatForegroundHelper.java +++ b/magicasakura/src/main/java/com/bilibili/magicasakura/widgets/AppCompatForegroundHelper.java @@ -36,11 +36,6 @@ * @time 16/4/7 */ public class AppCompatForegroundHelper extends AppCompatBaseHelper { - private static final int[] ATTR = { - android.R.attr.foreground, - R.attr.foregroundTint, - R.attr.foregroundTintMode - }; private TintInfo mForegroundTintInfo; @@ -54,15 +49,15 @@ public AppCompatForegroundHelper(View view, TintManager tintManager) { @SuppressWarnings("ResourceType") @Override void loadFromAttribute(AttributeSet attrs, int defStyleAttr) { - TypedArray array = mView.getContext().obtainStyledAttributes(attrs, ATTR, defStyleAttr, 0); - if (array.hasValue(1)) { - mForegroundTintResId = array.getResourceId(1, 0); - if (array.hasValue(2)) { - setSupportForegroundTintMode(DrawableUtils.parseTintMode(array.getInt(2, 0), null)); + TypedArray array = mView.getContext().obtainStyledAttributes(attrs, R.styleable.TintViewForegroundHelper, defStyleAttr, 0); + if (array.hasValue(R.styleable.TintViewForegroundHelper_foregroundTint)) { + mForegroundTintResId = array.getResourceId(R.styleable.TintViewForegroundHelper_foregroundTint, 0); + if (array.hasValue(R.styleable.TintViewForegroundHelper_foregroundTintMode)) { + setSupportForegroundTintMode(DrawableUtils.parseTintMode(array.getInt(R.styleable.TintViewForegroundHelper_foregroundTintMode, 0), null)); } setSupportForegroundTint(mForegroundTintResId); } else { - Drawable drawable = mTintManager.getDrawable(mForegroundResId = array.getResourceId(0, 0)); + Drawable drawable = mTintManager.getDrawable(mForegroundResId = array.getResourceId(R.styleable.TintViewForegroundHelper_android_foreground, 0)); if (drawable != null) { setForegroundDrawable(drawable); } diff --git a/magicasakura/src/main/java/com/bilibili/magicasakura/widgets/AppCompatImageHelper.java b/magicasakura/src/main/java/com/bilibili/magicasakura/widgets/AppCompatImageHelper.java index fd15a89..8ebf4f6 100644 --- a/magicasakura/src/main/java/com/bilibili/magicasakura/widgets/AppCompatImageHelper.java +++ b/magicasakura/src/main/java/com/bilibili/magicasakura/widgets/AppCompatImageHelper.java @@ -35,12 +35,6 @@ * @time 15/11/15 */ public class AppCompatImageHelper extends AppCompatBaseHelper { - public static final int[] ATTRS = { - android.R.attr.src, - R.attr.srcCompat, - R.attr.imageTint, - R.attr.imageTintMode - }; private TintInfo mImageTintInfo; private int mImageResId; @@ -53,23 +47,24 @@ public AppCompatImageHelper(View view, TintManager tintManager) { @SuppressWarnings("ResourceType") @Override void loadFromAttribute(AttributeSet attrs, int defStyleAttr) { - TypedArray array = mView.getContext().obtainStyledAttributes(attrs, ATTRS, defStyleAttr, 0); - Drawable image = mTintManager.getDrawable(mImageResId = array.getResourceId(1, 0)); - if (image != null) { - setImageDrawable(image); + TypedArray array = mView.getContext().obtainStyledAttributes(attrs, R.styleable.TintImageHelper, defStyleAttr, 0); + // first resolve srcCompat due to not extending by AppCompatImageView + if (((ImageView) mView).getDrawable() == null) { + Drawable image = mTintManager.getDrawable(mImageResId = array.getResourceId(R.styleable.TintImageHelper_srcCompat, 0)); + if (image != null) { + setImageDrawable(image); + } } - if (array.hasValue(2)) { - mImageTintResId = array.getResourceId(2, 0); - if (array.hasValue(3)) { - setSupportImageTintMode(DrawableUtils.parseTintMode(array.getInt(3, 0), null)); + if (array.hasValue(R.styleable.TintImageHelper_imageTint)) { + mImageTintResId = array.getResourceId(R.styleable.TintImageHelper_imageTint, 0); + if (array.hasValue(R.styleable.TintImageHelper_imageTintMode)) { + setSupportImageTintMode(DrawableUtils.parseTintMode(array.getInt(R.styleable.TintImageHelper_imageTintMode, 0), null)); } setSupportImageTint(mImageTintResId); - } else { - if (image == null) { - image = mTintManager.getDrawable(mImageResId = array.getResourceId(0, 0)); - if (image != null) { - setImageDrawable(image); - } + } else if (mImageResId == 0) { + Drawable image = mTintManager.getDrawable(mImageResId = array.getResourceId(R.styleable.TintImageHelper_android_src, 0)); + if (image != null) { + setImageDrawable(image); } } array.recycle(); diff --git a/magicasakura/src/main/java/com/bilibili/magicasakura/widgets/AppCompatProgressBarHelper.java b/magicasakura/src/main/java/com/bilibili/magicasakura/widgets/AppCompatProgressBarHelper.java index 43913f8..00c1d73 100644 --- a/magicasakura/src/main/java/com/bilibili/magicasakura/widgets/AppCompatProgressBarHelper.java +++ b/magicasakura/src/main/java/com/bilibili/magicasakura/widgets/AppCompatProgressBarHelper.java @@ -35,10 +35,6 @@ * @time 16/2/4 */ public class AppCompatProgressBarHelper extends AppCompatBaseHelper { - private static final int ATTR[] = new int[]{ - R.attr.progressTint, - R.attr.progressIndeterminateTint - }; private int mProgressTintResId; private int mIndeterminateTintResId; @@ -53,14 +49,14 @@ public AppCompatProgressBarHelper(View view, TintManager tintManager) { @SuppressWarnings("ResourceType") @Override void loadFromAttribute(AttributeSet attrs, int defStyleAttr) { - TypedArray array = mView.getContext().obtainStyledAttributes(attrs, ATTR, defStyleAttr, 0); - if (array.hasValue(0)) { - mProgressTintResId = array.getResourceId(0, 0); - setSupportProgressTint(array.getColorStateList(0)); + TypedArray array = mView.getContext().obtainStyledAttributes(attrs, R.styleable.TintProgressBarHelper, defStyleAttr, 0); + if (array.hasValue(R.styleable.TintProgressBarHelper_progressTint)) { + mProgressTintResId = array.getResourceId(R.styleable.TintProgressBarHelper_progressTint, 0); + setSupportProgressTint(array.getColorStateList(R.styleable.TintProgressBarHelper_progressTint)); } - if (array.hasValue(1)) { - mIndeterminateTintResId = array.getResourceId(1, 0); - setSupportIndeterminateTint(array.getColorStateList(1)); + if (array.hasValue(R.styleable.TintProgressBarHelper_progressIndeterminateTint)) { + mIndeterminateTintResId = array.getResourceId(R.styleable.TintProgressBarHelper_progressIndeterminateTint, 0); + setSupportIndeterminateTint(array.getColorStateList(R.styleable.TintProgressBarHelper_progressIndeterminateTint)); } array.recycle(); } diff --git a/magicasakura/src/main/java/com/bilibili/magicasakura/widgets/AppCompatTextHelper.java b/magicasakura/src/main/java/com/bilibili/magicasakura/widgets/AppCompatTextHelper.java index 0299e16..cd43e43 100644 --- a/magicasakura/src/main/java/com/bilibili/magicasakura/widgets/AppCompatTextHelper.java +++ b/magicasakura/src/main/java/com/bilibili/magicasakura/widgets/AppCompatTextHelper.java @@ -36,11 +36,6 @@ public class AppCompatTextHelper extends AppCompatBaseHelper { //If writing like this: //int[] ATTRS = { R.attr.tintText, android.R.attr.textColor, android.R.attr.textColorLink, ...}; //we can't get textColor value when api is below 20; - private static final int[] ATTRS = { - android.R.attr.textColor, - android.R.attr.textColorLink, - android.R.attr.textAppearance, - }; private int mTextColorId; private int mTextLinkColorId; @@ -55,17 +50,17 @@ public AppCompatTextHelper(View view, TintManager tintManager) { @SuppressWarnings("ResourceType") @Override void loadFromAttribute(AttributeSet attrs, int defStyleAttr) { - TypedArray array = mView.getContext().obtainStyledAttributes(attrs, ATTRS, defStyleAttr, 0); + TypedArray array = mView.getContext().obtainStyledAttributes(attrs, R.styleable.TintTextHelper , defStyleAttr, 0); - int textColorId = array.getResourceId(0, 0); + int textColorId = array.getResourceId(R.styleable.TintTextHelper_android_textColor, 0); if (textColorId == 0) { - setTextAppearanceForTextColor(array.getResourceId(2, 0), false); + setTextAppearanceForTextColor(array.getResourceId(R.styleable.TintTextHelper_android_textAppearance, 0), false); } else { setTextColor(textColorId); } - if (array.hasValue(1)) { - setLinkTextColor(array.getResourceId(1, 0)); + if (array.hasValue(R.styleable.TintTextHelper_android_textColorLink)) { + setLinkTextColor(array.getResourceId(R.styleable.TintTextHelper_android_textColorLink, 0)); } array.recycle(); } diff --git a/magicasakura/src/main/java/com/bilibili/magicasakura/widgets/TintSwitchCompat.java b/magicasakura/src/main/java/com/bilibili/magicasakura/widgets/TintSwitchCompat.java index c4445a0..b34272e 100644 --- a/magicasakura/src/main/java/com/bilibili/magicasakura/widgets/TintSwitchCompat.java +++ b/magicasakura/src/main/java/com/bilibili/magicasakura/widgets/TintSwitchCompat.java @@ -48,7 +48,7 @@ public TintSwitchCompat(Context context, AttributeSet attrs, int defStyleAttr) { } TintManager tintManager = TintManager.get(context); mThumbHelper = new AppCompatSwitchHelper(this, tintManager, - new int[]{android.R.attr.thumb, R.attr.thumbTint, R.attr.thumbTintMode}, + R.styleable.TintSwitchThumb, new AppCompatSwitchHelper.DrawableCallback() { @Override public void setDrawable(Drawable drawable) { @@ -63,7 +63,7 @@ public Drawable getDrawable() { mThumbHelper.loadFromAttribute(attrs, defStyleAttr); mTrackHelper = new AppCompatSwitchHelper(this, tintManager, - new int[]{R.attr.track, R.attr.trackTint, R.attr.trackTintMode}, + R.styleable.TintSwitchTrack, new AppCompatSwitchHelper.DrawableCallback() { @Override public void setDrawable(Drawable drawable) { diff --git a/magicasakura/src/main/res/values/attrs.xml b/magicasakura/src/main/res/values/attrs.xml index 471b786..c50074a 100644 --- a/magicasakura/src/main/res/values/attrs.xml +++ b/magicasakura/src/main/res/values/attrs.xml @@ -20,10 +20,17 @@ - + + + + + + + + @@ -32,6 +39,22 @@ + + + + + + + + + + + + + + + + @@ -41,6 +64,10 @@ + + + + @@ -49,6 +76,7 @@ + @@ -57,6 +85,7 @@ + @@ -65,6 +94,7 @@ + @@ -73,14 +103,32 @@ - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -89,10 +137,10 @@ - - + +