-
public final class DrawableCompat
Helper for accessing features in Drawable.
-
-
Method Summary
Modifier and Type Method Description static void
jumpToCurrentState(@NonNull() Drawable drawable)
Call Drawable.jumpToCurrentState(). static void
setAutoMirrored(@NonNull() Drawable drawable, boolean mirrored)
Set whether this Drawable is automatically mirrored when its layoutdirection is RTL (right-to left). static boolean
isAutoMirrored(@NonNull() Drawable drawable)
Tells if this Drawable will be automatically mirrored when its layoutdirection is RTL right-to-left. static void
setHotspot(@NonNull() Drawable drawable, float x, float y)
Specifies the hotspot's location within the drawable. static void
setHotspotBounds(@NonNull() Drawable drawable, int left, int top, int right, int bottom)
Sets the bounds to which the hotspot is constrained, if they should bedifferent from the drawable bounds. static void
setTint(@NonNull() Drawable drawable, @ColorInt() int tint)
Specifies a tint for {@code drawable}
.static void
setTintList(@NonNull() Drawable drawable, @Nullable() ColorStateList tint)
Specifies a tint for {@code drawable}
as a color state list.static void
setTintMode(@NonNull() Drawable drawable, @NonNull() PorterDuff.Mode tintMode)
Specifies a tint blending mode for {@code drawable}
.static int
getAlpha(@NonNull() Drawable drawable)
Get the alpha value of the {@code drawable}
.0 means fully transparent, 255 means fully opaque.static void
applyTheme(@NonNull() Drawable drawable, @NonNull() Resources.Theme theme)
Applies the specified theme to this Drawable and its children. static boolean
canApplyTheme(@NonNull() Drawable drawable)
Whether a theme can be applied to this Drawable and its children. static ColorFilter
getColorFilter(@NonNull() Drawable drawable)
Returns the current color filter, or {@code null}
if none set.static void
clearColorFilter(@NonNull() Drawable drawable)
Removes the color filter from the given drawable. static void
inflate(@NonNull() Drawable drawable, @NonNull() Resources res, @NonNull() XmlPullParser parser, @NonNull() AttributeSet attrs, @Nullable() Resources.Theme theme)
Inflate this Drawable from an XML resource optionally styled by a theme. static Drawable
wrap(@NonNull() Drawable drawable)
Potentially wrap {@code drawable}
so that it may be used for tinting across thedifferent API levels, via the tinting methods in this class.static <T extends Drawable> T
unwrap(@NonNull() Drawable drawable)
Unwrap {@code drawable}
if it is the result of a call to wrap.static boolean
setLayoutDirection(@NonNull() Drawable drawable, int layoutDirection)
Set the layout direction for this drawable. static int
getLayoutDirection(@NonNull() Drawable drawable)
Returns the resolved layout direction for this Drawable. -
-
Method Detail
-
jumpToCurrentState
@Deprecated() static void jumpToCurrentState(@NonNull() Drawable drawable)
- Parameters:
drawable
- The Drawable against which to invoke the method.
-
setAutoMirrored
static void setAutoMirrored(@NonNull() Drawable drawable, boolean mirrored)
Set whether this Drawable is automatically mirrored when its layoutdirection is RTL (right-to left). See android.util.LayoutDirection.
If running on a pre-KITKAT devicethis method does nothing.
- Parameters:
drawable
- The Drawable against which to invoke the method.mirrored
- Set to true if the Drawable should be mirrored, false ifnot.
-
isAutoMirrored
static boolean isAutoMirrored(@NonNull() Drawable drawable)
Tells if this Drawable will be automatically mirrored when its layoutdirection is RTL right-to-left. See android.util.LayoutDirection.
If running on a pre-KITKAT devicethis method returns false.
- Parameters:
drawable
- The Drawable against which to invoke the method.
-
setHotspot
static void setHotspot(@NonNull() Drawable drawable, float x, float y)
Specifies the hotspot's location within the drawable.
- Parameters:
drawable
- The Drawable against which to invoke the method.x
- The X coordinate of the center of the hotspoty
- The Y coordinate of the center of the hotspot
-
setHotspotBounds
static void setHotspotBounds(@NonNull() Drawable drawable, int left, int top, int right, int bottom)
Sets the bounds to which the hotspot is constrained, if they should bedifferent from the drawable bounds.
- Parameters:
drawable
- The Drawable against which to invoke the method.
-
setTint
static void setTint(@NonNull() Drawable drawable, @ColorInt() int tint)
Specifies a tint for
{@code drawable}
.- Parameters:
drawable
- The Drawable against which to invoke the method.tint
- Color to use for tinting this drawable
-
setTintList
static void setTintList(@NonNull() Drawable drawable, @Nullable() ColorStateList tint)
Specifies a tint for
{@code drawable}
as a color state list.- Parameters:
drawable
- The Drawable against which to invoke the method.tint
- Color state list to use for tinting this drawable, or null to clear the tint
-
setTintMode
static void setTintMode(@NonNull() Drawable drawable, @NonNull() PorterDuff.Mode tintMode)
Specifies a tint blending mode for
{@code drawable}
.- Parameters:
drawable
- The Drawable against which to invoke the method.tintMode
- A Porter-Duff blending mode
-
getAlpha
static int getAlpha(@NonNull() Drawable drawable)
Get the alpha value of the
{@code drawable}
.0 means fully transparent, 255 means fully opaque.- Parameters:
drawable
- The Drawable against which to invoke the method.
-
applyTheme
static void applyTheme(@NonNull() Drawable drawable, @NonNull() Resources.Theme theme)
Applies the specified theme to this Drawable and its children.
-
canApplyTheme
static boolean canApplyTheme(@NonNull() Drawable drawable)
Whether a theme can be applied to this Drawable and its children.
-
getColorFilter
static ColorFilter getColorFilter(@NonNull() Drawable drawable)
Returns the current color filter, or
{@code null}
if none set.
-
clearColorFilter
static void clearColorFilter(@NonNull() Drawable drawable)
Removes the color filter from the given drawable.
-
inflate
static void inflate(@NonNull() Drawable drawable, @NonNull() Resources res, @NonNull() XmlPullParser parser, @NonNull() AttributeSet attrs, @Nullable() Resources.Theme theme)
Inflate this Drawable from an XML resource optionally styled by a theme.
- Parameters:
res
- Resources used to resolve attribute valuesparser
- XML parser from which to inflate this Drawableattrs
- Base set of attribute valuestheme
- Theme to apply, may be null
-
wrap
static Drawable wrap(@NonNull() Drawable drawable)
Potentially wrap
{@code drawable}
so that it may be used for tinting across thedifferent API levels, via the tinting methods in this class.If the given drawable is wrapped, we will copy over certain state over to the wrappeddrawable, such as its bounds, level, visibility and state.
You must use the result of this call. If the given drawable is being used by a view(as its background for instance), you must replace the original drawable withthe result of this call:
Drawable bg = DrawableCompat.wrap(view.getBackground()); // Need to set the background with the wrapped drawable view.setBackground(bg); // You can now tint the drawable DrawableCompat.setTint(bg, ...);
If you need to get hold of the original Drawable again,you can use the value returned from unwrap.
- Parameters:
drawable
- The Drawable to process
-
unwrap
static <T extends Drawable> T unwrap(@NonNull() Drawable drawable)
Unwrap
{@code drawable}
if it is the result of a call to wrap. Ifthe{@code drawable}
is not the result of a call to wrap then{@code drawable}
is returned as-is.- Parameters:
drawable
- The drawable to unwrap
-
setLayoutDirection
static boolean setLayoutDirection(@NonNull() Drawable drawable, int layoutDirection)
Set the layout direction for this drawable. Should be a resolvedlayout direction, as the Drawable has no capacity to do the resolution onits own.
- Parameters:
layoutDirection
- the resolved layout direction for the drawable,either LAYOUT_DIRECTION_LTR or LAYOUT_DIRECTION_RTL
-
getLayoutDirection
static int getLayoutDirection(@NonNull() Drawable drawable)
Returns the resolved layout direction for this Drawable.
-
-
-
-