From c3a1149cb682e4ddbc83e2a68a2178a188d936b9 Mon Sep 17 00:00:00 2001 From: Manuel Martin Date: Fri, 17 Apr 2020 18:03:30 +0200 Subject: [PATCH] Better handle UIButton drawable states (#3186) --- .../mozilla/vrbrowser/ui/views/UIButton.java | 37 +++++++++---------- 1 file changed, 18 insertions(+), 19 deletions(-) diff --git a/app/src/common/shared/org/mozilla/vrbrowser/ui/views/UIButton.java b/app/src/common/shared/org/mozilla/vrbrowser/ui/views/UIButton.java index 433f20f21..cec724e8a 100644 --- a/app/src/common/shared/org/mozilla/vrbrowser/ui/views/UIButton.java +++ b/app/src/common/shared/org/mozilla/vrbrowser/ui/views/UIButton.java @@ -57,7 +57,6 @@ public class UIButton extends AppCompatImageButton implements CustomUIButton { private boolean mIsActive; private boolean mIsNotification; private ClipDrawable mClipDrawable; - private Drawable mDrawable; private int mClipColor; public UIButton(Context context, AttributeSet attrs) { @@ -70,9 +69,6 @@ public UIButton(Context context, AttributeSet attrs, int defStyleAttr) { TypedArray attributes = context.obtainStyledAttributes(attrs, R.styleable.UIButton, defStyleAttr, 0); mTintColorListRes = attributes.getResourceId(R.styleable.UIButton_tintColorList, 0); - if (mTintColorListRes != 0) { - setTintColorList(mTintColorListRes); - } mBackground = attributes.getDrawable(R.styleable.UIButton_regularModeBackground); mPrivateModeBackground = attributes.getDrawable(R.styleable.UIButton_privateModeBackground); mActiveModeBackground = attributes.getDrawable(R.styleable.UIButton_activeModeBackground); @@ -98,10 +94,12 @@ public UIButton(Context context, AttributeSet attrs, int defStyleAttr) { } if (mClipDrawable != null) { - Drawable[] layers = new Drawable[] { mDrawable, mClipDrawable }; + Drawable[] layers = new Drawable[] { getDrawable(), mClipDrawable }; setImageDrawable(new LayerDrawable(layers)); - mClipDrawable.setLevel(0); - mClipDrawable.setTint(R.color.azure); + } + + if (mTintColorListRes != 0) { + setTintColorList(mTintColorListRes); } // Android >8 doesn't perform a click when long clicking in ImageViews even if long click is disabled @@ -179,30 +177,25 @@ public boolean onHoverEvent(MotionEvent event) { } public void setTintColorList(int aColorListId) { - if (mDrawable == null) { + if (getDrawable() == null) { return; } mTintColorList = getContext().getResources().getColorStateList( aColorListId, getContext().getTheme()); - int color = mTintColorList.getColorForState(getDrawableState(), 0); - mDrawable.setColorFilter(new PorterDuffColorFilter(color, PorterDuff.Mode.MULTIPLY)); - if (mClipDrawable != null) { - mClipDrawable.setColorFilter(new PorterDuffColorFilter(mClipColor, PorterDuff.Mode.MULTIPLY)); - } + refreshDrawableState(); } @Override protected void drawableStateChanged() { super.drawableStateChanged(); - if (mDrawable == null) { - return; - } if (mTintColorList != null && mTintColorList.isStateful()) { int color = mTintColorList.getColorForState(getDrawableState(), 0); - mDrawable.setColorFilter(new PorterDuffColorFilter(color, PorterDuff.Mode.MULTIPLY)); + getDrawable().setColorFilter(new PorterDuffColorFilter(color, PorterDuff.Mode.MULTIPLY)); if (mClipDrawable != null) { mClipDrawable.setColorFilter(new PorterDuffColorFilter(mClipColor, PorterDuff.Mode.MULTIPLY)); + } else { + setColorFilter(color); } } } @@ -368,8 +361,14 @@ public void setEnabled(boolean enabled) { @Override public void setImageDrawable(@Nullable Drawable drawable) { - super.setImageDrawable(drawable); - mDrawable = drawable; + Drawable image = drawable; + if (mClipDrawable != null) { + Drawable[] layers = new Drawable[] { drawable, mClipDrawable }; + image = new LayerDrawable(layers); + mClipDrawable.setLevel(0); + mClipDrawable.setTint(getResources().getColor(R.color.azure, getContext().getTheme())); + } + super.setImageDrawable(image); updateButtonColor(); }