Skip to content

Commit

Permalink
optimize drawable helper
Browse files Browse the repository at this point in the history
  • Loading branch information
xyczero committed Nov 6, 2017
1 parent a80ca4c commit 0d25534
Show file tree
Hide file tree
Showing 16 changed files with 44 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
* @author xyczero617@gmail.com
* @time 15/9/26
*/
public class AppCompatBackgroundHelper extends AppCompatBaseHelper {
public class AppCompatBackgroundHelper extends AppCompatBaseHelper<View> {

private TintInfo mBackgroundTintInfo;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@
* @author xyczero617@gmail.com
* @time 15/10/1
*/
public abstract class AppCompatBaseHelper {
protected View mView;
public abstract class AppCompatBaseHelper<T extends View> {
protected T mView;
protected TintManager mTintManager;

private boolean mSkipNextApply;

public AppCompatBaseHelper(View view, TintManager tintManager) {
public AppCompatBaseHelper(T view, TintManager tintManager) {
mView = view;
mTintManager = tintManager;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
* @author xyczero617@gmail.com
* @time 15/11/23
*/
public class AppCompatCompoundButtonHelper extends AppCompatBaseHelper {
public class AppCompatCompoundButtonHelper extends AppCompatBaseHelper<CompoundButton> {

private TintInfo mCompoundButtonTintInfo;
private int mCompoundButtonResId;
Expand Down Expand Up @@ -106,7 +106,7 @@ public void setButtonDrawableTintList(int resId, PorterDuff.Mode mode) {
private void setButtonDrawable(Drawable drawable) {
if (skipNextApply()) return;

((CompoundButton) mView).setButtonDrawable(drawable);
mView.setButtonDrawable(drawable);
}

public boolean setSupportButtonDrawableTint(int resId) {
Expand All @@ -131,7 +131,7 @@ private void setSupportButtonDrawableTintMode(PorterDuff.Mode mode) {
}

public boolean applySupportButtonDrawableTint() {
Drawable buttonDrawable = CompoundButtonCompat.getButtonDrawable((CompoundButton) mView);
Drawable buttonDrawable = CompoundButtonCompat.getButtonDrawable(mView);
if (buttonDrawable != null && mCompoundButtonTintInfo != null && mCompoundButtonTintInfo.mHasTintList) {
buttonDrawable = DrawableCompat.wrap(buttonDrawable);
buttonDrawable = buttonDrawable.mutate();
Expand Down Expand Up @@ -165,7 +165,7 @@ public int getCompoundPaddingLeft(int superValue) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) {
// Before JB-MR1 the button drawable wasn't taken into account for padding. We'll
// workaround that here
Drawable buttonDrawable = CompoundButtonCompat.getButtonDrawable((CompoundButton) mView);
Drawable buttonDrawable = CompoundButtonCompat.getButtonDrawable(mView);
if (buttonDrawable != null) {
superValue += buttonDrawable.getIntrinsicWidth();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,14 @@

import com.bilibili.magicasakura.R;
import com.bilibili.magicasakura.utils.DrawableUtils;
import com.bilibili.magicasakura.utils.ThemeUtils;
import com.bilibili.magicasakura.utils.TintInfo;
import com.bilibili.magicasakura.utils.TintManager;

/**
* @author xyczero617@gmail.com
* @time 15/9/26
*/
public class AppCompatCompoundDrawableHelper extends AppCompatBaseHelper {
public class AppCompatCompoundDrawableHelper extends AppCompatBaseHelper<TextView> {

private TintInfo[] mCompoundDrawableTintInfos = new TintInfo[4];

Expand Down Expand Up @@ -123,7 +122,7 @@ public void setCompoundDrawablesTintList(int... resIds) {
private void setCompoundDrawablesWithIntrinsicBounds(Drawable left, Drawable top, Drawable right, Drawable bottom) {
if (skipNextApply()) return;

((TextView) mView).setCompoundDrawablesWithIntrinsicBounds(left, top, right, bottom);
mView.setCompoundDrawablesWithIntrinsicBounds(left, top, right, bottom);
}

private Drawable getCompoundDrawableByPosition(int i) {
Expand Down Expand Up @@ -164,7 +163,7 @@ private void setSupportCompoundDrawableTintModeByPosition(int position, PorterDu
}

private Drawable applySupportCompoundDrawableTint(int position) {
Drawable originDrawable = ((TextView) mView).getCompoundDrawables()[position];
Drawable originDrawable = mView.getCompoundDrawables()[position];
Drawable compoundDrawable = originDrawable;
TintInfo tintInfo = mCompoundDrawableTintInfos[position];
if (compoundDrawable != null && tintInfo != null && tintInfo.mHasTintList) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
* @author xyczero617@gmail.com
* @time 16/4/7
*/
public class AppCompatForegroundHelper extends AppCompatBaseHelper {
public class AppCompatForegroundHelper extends AppCompatBaseHelper<View> {

private TintInfo mForegroundTintInfo;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import android.support.v4.content.ContextCompat;
import android.support.v4.graphics.drawable.DrawableCompat;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;

import com.bilibili.magicasakura.R;
Expand All @@ -34,13 +33,13 @@
* @author xyczero617@gmail.com
* @time 15/11/15
*/
public class AppCompatImageHelper extends AppCompatBaseHelper {
public class AppCompatImageHelper extends AppCompatBaseHelper<ImageView> {

private TintInfo mImageTintInfo;
private int mImageResId;
private int mImageTintResId;

public AppCompatImageHelper(View view, TintManager tintManager) {
public AppCompatImageHelper(ImageView view, TintManager tintManager) {
super(view, tintManager);
}

Expand All @@ -49,7 +48,7 @@ public AppCompatImageHelper(View view, TintManager tintManager) {
void loadFromAttribute(AttributeSet attrs, int defStyleAttr) {
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) {
if (mView.getDrawable() == null) {
Drawable image = mTintManager.getDrawable(mImageResId = array.getResourceId(R.styleable.TintImageHelper_srcCompat, 0));
if (image != null) {
setImageDrawable(image);
Expand Down Expand Up @@ -109,7 +108,7 @@ public void setImageTintList(int resId, PorterDuff.Mode mode) {
private void setImageDrawable(Drawable drawable) {
if (skipNextApply()) return;

((ImageView) mView).setImageDrawable(drawable);
mView.setImageDrawable(drawable);
}

private boolean setSupportImageTint(int resId) {
Expand All @@ -134,7 +133,7 @@ private void setSupportImageTintMode(PorterDuff.Mode mode) {
}

private boolean applySupportImageTint() {
Drawable image = ((ImageView) mView).getDrawable();
Drawable image = mView.getDrawable();
if (image != null && mImageTintInfo != null && mImageTintInfo.mHasTintList) {
Drawable tintDrawable = image.mutate();
tintDrawable = DrawableCompat.wrap(tintDrawable);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,15 @@
* @author xyczero617@gmail.com
* @time 16/2/4
*/
public class AppCompatProgressBarHelper extends AppCompatBaseHelper {
public class AppCompatProgressBarHelper extends AppCompatBaseHelper<TintProgressBar> {

private int mProgressTintResId;
private int mIndeterminateTintResId;

private TintInfo mProgressTintInfo;
private TintInfo mIndeterminateTintInfo;

public AppCompatProgressBarHelper(View view, TintManager tintManager) {
public AppCompatProgressBarHelper(TintProgressBar view, TintManager tintManager) {
super(view, tintManager);
}

Expand Down Expand Up @@ -99,11 +99,11 @@ private void applySupportProgressTint() {
}

private void applySupportIndeterminateTint() {
Drawable mIndeterminateDrawable = ((ProgressBar) mView).getIndeterminateDrawable();
Drawable mIndeterminateDrawable = mView.getIndeterminateDrawable();
if (mIndeterminateDrawable != null && mIndeterminateTintInfo != null) {
final TintInfo tintInfo = mIndeterminateTintInfo;
if (tintInfo.mHasTintList || tintInfo.mHasTintMode) {
((ProgressBar) mView).setIndeterminateDrawable(mIndeterminateDrawable = mIndeterminateDrawable.mutate());
mView.setIndeterminateDrawable(mIndeterminateDrawable = mIndeterminateDrawable.mutate());
TintManager.tintViewDrawable(mView, mIndeterminateDrawable, mIndeterminateTintInfo);
// The drawable (or one of its children) may not have been
// stateful before applying the tint, so let's try again.
Expand All @@ -118,9 +118,9 @@ private void applySupportIndeterminateTint() {
private Drawable getTintTarget(int layerId, boolean shouldFallback) {
Drawable layer = null;

final Drawable d = ((ProgressBar) mView).getProgressDrawable();
final Drawable d = mView.getProgressDrawable();
if (d != null) {
((ProgressBar) mView).setProgressDrawable(d.mutate());
mView.setProgressDrawable(d.mutate());

if (d instanceof LayerDrawable) {
layer = ((LayerDrawable) d).findDrawableByLayerId(layerId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
* @author xyczero617@gmail.com
* @time 15/9/26
*/
public class AppCompatTextHelper extends AppCompatBaseHelper {
public class AppCompatTextHelper extends AppCompatBaseHelper<TextView> {

//If writing like this:
//int[] ATTRS = { R.attr.tintText, android.R.attr.textColor, android.R.attr.textColorLink, ...};
Expand All @@ -43,7 +43,7 @@ public class AppCompatTextHelper extends AppCompatBaseHelper {
private TintInfo mTextColorTintInfo;
private TintInfo mTextLinkColorTintInfo;

public AppCompatTextHelper(View view, TintManager tintManager) {
public AppCompatTextHelper(TextView view, TintManager tintManager) {
super(view, tintManager);
}

Expand Down Expand Up @@ -111,7 +111,7 @@ public void setTextColorById(@ColorRes int colorId) {
private void setTextColor(ColorStateList tint) {
if (skipNextApply()) return;

((TextView) mView).setTextColor(tint);
mView.setTextColor(tint);
}

private void setTextColor(@ColorRes int resId) {
Expand Down Expand Up @@ -164,7 +164,7 @@ private void applySupportTextColorTint() {

private void applySupportTextLinkColorTint() {
if (mTextLinkColorTintInfo != null && mTextLinkColorTintInfo.mHasTintList) {
((TextView) mView).setLinkTextColor(mTextLinkColorTintInfo.mTintList);
mView.setLinkTextColor(mTextLinkColorTintInfo.mTintList);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.bilibili.magicasakura.widgets;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.PorterDuff;
import android.graphics.drawable.Drawable;
Expand All @@ -28,6 +29,7 @@
* @author xyczero617@gmail.com
* @time 16/2/14
*/
@SuppressLint("AppCompatCustomView")
public class TintAutoCompleteTextView extends AutoCompleteTextView implements Tintable, AppCompatBackgroundHelper.BackgroundExtensible,
AppCompatCompoundDrawableHelper.CompoundDrawableExtensible {
private AppCompatBackgroundHelper mBackgroundHelper;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.bilibili.magicasakura.widgets;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.content.Context;
import android.content.res.ColorStateList;
Expand All @@ -32,6 +33,8 @@
* @author xyczero617@gmail.com
* @time 15/10/25
*/

@SuppressLint("AppCompatCustomView")
public class TintButton extends Button implements Tintable, AppCompatBackgroundHelper.BackgroundExtensible,
AppCompatTextHelper.TextExtensible {
private AppCompatTextHelper mTextHelper;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.bilibili.magicasakura.widgets;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.content.Context;
import android.content.res.ColorStateList;
Expand All @@ -37,6 +38,7 @@
* @author xyczero617@gmail.com
* @time 16/1/27
*/
@SuppressLint("AppCompatCustomView")
public class TintCheckBox extends CheckBox implements Tintable, AppCompatBackgroundHelper.BackgroundExtensible,
AppCompatCompoundButtonHelper.CompoundButtonExtensible, AppCompatTextHelper.TextExtensible {
private AppCompatBackgroundHelper mBackgroundHelper;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.bilibili.magicasakura.widgets;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.PorterDuff;
Expand All @@ -36,6 +37,7 @@
* special view for replacing view in preference , not recommend to use it in common
* layout: select_dialog_singlechoice_xxx
*/
@SuppressLint("AppCompatCustomView")
public class TintCheckedTextView extends CheckedTextView implements Tintable {
private static final int[] ATTRS = {
android.R.attr.drawableLeft,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.bilibili.magicasakura.widgets;

import android.annotation.TargetApi;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.ColorStateList;
import android.graphics.PorterDuff;
Expand All @@ -35,6 +36,7 @@
* @author xyczero617@gmail.com
* @time 16/2/1
*/
@SuppressLint("AppCompatCustomView")
public class TintEditText extends EditText implements Tintable, AppCompatBackgroundHelper.BackgroundExtensible,
AppCompatCompoundDrawableHelper.CompoundDrawableExtensible, AppCompatTextHelper.TextExtensible {
private AppCompatBackgroundHelper mBackgroundHelper;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.bilibili.magicasakura.widgets;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.PorterDuff;
import android.graphics.drawable.Drawable;
Expand All @@ -28,6 +29,7 @@
* @author xyczero617@gmail.com
* @time 15/11/8
*/
@SuppressLint("AppCompatCustomView")
public class TintImageView extends ImageView implements Tintable, AppCompatBackgroundHelper.BackgroundExtensible,
AppCompatImageHelper.ImageExtensible {
private AppCompatBackgroundHelper mBackgroundHelper;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.bilibili.magicasakura.widgets;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.content.Context;
import android.content.res.ColorStateList;
Expand All @@ -37,6 +38,7 @@
* @author xyczero617@gmail.com
* @time 16/1/19
*/
@SuppressLint("AppCompatCustomView")
public class TintRadioButton extends RadioButton implements Tintable, AppCompatBackgroundHelper.BackgroundExtensible
, AppCompatCompoundButtonHelper.CompoundButtonExtensible, AppCompatTextHelper.TextExtensible {
private AppCompatTextHelper mTextHelper;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.bilibili.magicasakura.widgets;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.content.Context;
import android.content.res.ColorStateList;
Expand All @@ -35,6 +36,7 @@
* @author xyczero617@gmail.com
* @time 15/9/14
*/
@SuppressLint("AppCompatCustomView")
public class TintTextView extends TextView implements Tintable, AppCompatBackgroundHelper.BackgroundExtensible,
AppCompatCompoundDrawableHelper.CompoundDrawableExtensible, AppCompatTextHelper.TextExtensible {
private AppCompatTextHelper mTextHelper;
Expand Down

0 comments on commit 0d25534

Please sign in to comment.