diff options
| author | Nader Jawad <njawad@google.com> | 2019-04-14 21:58:04 -0700 |
|---|---|---|
| committer | Nader Jawad <njawad@google.com> | 2019-04-15 17:01:56 -0700 |
| commit | 8e31c3ef143f391b2360fd8f16ea68af9151d122 (patch) | |
| tree | d252378c33820cffc870ffbdd0116648d91ae376 /core/java/android/widget/CompoundButton.java | |
| parent | b9a4f633390ec9ed51296c74887ee08b0056eff8 (diff) | |
Added BlendMode equivalent APIs to replace deprecated PorterDuff
variants
Updated various framework Views to have equivalent BlendMode APIs
to replace the deprecated PorterDuff equivalents.
Updated InspectableProperty annotations to refer to the same
xml attributes as the original tintmode APIs
Bug: 126726419
Test: Added CTS tests to verify new BlendMode APIs
Change-Id: Id9ab36d3d4d29f351250723e9d13d49bc6062c83
Diffstat (limited to 'core/java/android/widget/CompoundButton.java')
| -rw-r--r-- | core/java/android/widget/CompoundButton.java | 53 |
1 files changed, 41 insertions, 12 deletions
diff --git a/core/java/android/widget/CompoundButton.java b/core/java/android/widget/CompoundButton.java index 80ec1c65293b..2674ca4d159a 100644 --- a/core/java/android/widget/CompoundButton.java +++ b/core/java/android/widget/CompoundButton.java @@ -23,6 +23,7 @@ import android.annotation.UnsupportedAppUsage; import android.content.Context; import android.content.res.ColorStateList; import android.content.res.TypedArray; +import android.graphics.BlendMode; import android.graphics.Canvas; import android.graphics.PorterDuff; import android.graphics.drawable.Drawable; @@ -67,9 +68,9 @@ public abstract class CompoundButton extends Button implements Checkable { @UnsupportedAppUsage private Drawable mButtonDrawable; private ColorStateList mButtonTintList = null; - private PorterDuff.Mode mButtonTintMode = null; + private BlendMode mButtonBlendMode = null; private boolean mHasButtonTint = false; - private boolean mHasButtonTintMode = false; + private boolean mHasButtonBlendMode = false; @UnsupportedAppUsage private OnCheckedChangeListener mOnCheckedChangeListener; @@ -109,9 +110,9 @@ public abstract class CompoundButton extends Button implements Checkable { } if (a.hasValue(R.styleable.CompoundButton_buttonTintMode)) { - mButtonTintMode = Drawable.parseTintMode(a.getInt( - R.styleable.CompoundButton_buttonTintMode, -1), mButtonTintMode); - mHasButtonTintMode = true; + mButtonBlendMode = Drawable.parseBlendMode(a.getInt( + R.styleable.CompoundButton_buttonTintMode, -1), mButtonBlendMode); + mHasButtonBlendMode = true; } if (a.hasValue(R.styleable.CompoundButton_buttonTint)) { @@ -337,8 +338,23 @@ public abstract class CompoundButton extends Button implements Checkable { * @see Drawable#setTintMode(PorterDuff.Mode) */ public void setButtonTintMode(@Nullable PorterDuff.Mode tintMode) { - mButtonTintMode = tintMode; - mHasButtonTintMode = true; + setButtonTintBlendMode(tintMode != null ? BlendMode.fromValue(tintMode.nativeInt) : null); + } + + /** + * Specifies the blending mode used to apply the tint specified by + * {@link #setButtonTintList(ColorStateList)}} to the button drawable. The + * default mode is {@link PorterDuff.Mode#SRC_IN}. + * + * @param tintMode the blending mode used to apply the tint, may be + * {@code null} to clear tint + * @attr ref android.R.styleable#CompoundButton_buttonTintMode + * @see #getButtonTintMode() + * @see Drawable#setTintBlendMode(BlendMode) + */ + public void setButtonTintBlendMode(@Nullable BlendMode tintMode) { + mButtonBlendMode = tintMode; + mHasButtonBlendMode = true; applyButtonTint(); } @@ -348,22 +364,35 @@ public abstract class CompoundButton extends Button implements Checkable { * @attr ref android.R.styleable#CompoundButton_buttonTintMode * @see #setButtonTintMode(PorterDuff.Mode) */ - @InspectableProperty + @InspectableProperty(name = "buttonTintMode") @Nullable public PorterDuff.Mode getButtonTintMode() { - return mButtonTintMode; + return mButtonBlendMode != null ? BlendMode.blendModeToPorterDuffMode(mButtonBlendMode) : + null; + } + + /** + * @return the blending mode used to apply the tint to the button drawable + * @attr ref android.R.styleable#CompoundButton_buttonTintMode + * @see #setButtonTintBlendMode(BlendMode) + */ + @InspectableProperty(name = "buttonBlendMode", + attributeId = R.styleable.CompoundButton_buttonTintMode) + @Nullable + public BlendMode getButtonTintBlendMode() { + return mButtonBlendMode; } private void applyButtonTint() { - if (mButtonDrawable != null && (mHasButtonTint || mHasButtonTintMode)) { + if (mButtonDrawable != null && (mHasButtonTint || mHasButtonBlendMode)) { mButtonDrawable = mButtonDrawable.mutate(); if (mHasButtonTint) { mButtonDrawable.setTintList(mButtonTintList); } - if (mHasButtonTintMode) { - mButtonDrawable.setTintMode(mButtonTintMode); + if (mHasButtonBlendMode) { + mButtonDrawable.setTintBlendMode(mButtonBlendMode); } // The drawable (or one of its children) may not have been |
