DrawableCompat

Added in 1.1.0

public final class DrawableCompat


Helper for accessing features in android.graphics.drawable.Drawable.

Summary

Public methods

static void

Applies the specified theme to this Drawable and its children.

static boolean

Whether a theme can be applied to this Drawable and its children.

static void

Removes the color filter from the given drawable.

static int
@ReplaceWith(expression = "drawable.getAlpha()")
getAlpha(@NonNull Drawable drawable)

This method is deprecated.

Call getAlpha directly.

static @Nullable ColorFilter

Returns the current color filter, or null if none set.

static int

Returns the resolved layout direction for this 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 boolean
@ReplaceWith(expression = "drawable.isAutoMirrored()")
isAutoMirrored(@NonNull Drawable drawable)

This method is deprecated.

Call isAutoMirrored directly.

static void
@ReplaceWith(expression = "drawable.jumpToCurrentState()")
jumpToCurrentState(@NonNull Drawable drawable)

This method is deprecated.

Use jumpToCurrentState directly.

static void
@ReplaceWith(expression = "drawable.setAutoMirrored(mirrored)")
setAutoMirrored(@NonNull Drawable drawable, boolean mirrored)

This method is deprecated.

Call setAutoMirrored directly.

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 be different from the drawable bounds.

static boolean
setLayoutDirection(@NonNull Drawable drawable, int layoutDirection)

Set the layout direction for this drawable.

static void
setTint(@NonNull Drawable drawable, @ColorInt int tint)

Specifies a tint for drawable.

static void

Specifies a tint for drawable as a color state list.

static void

Specifies a tint blending mode for drawable.

static T
<T extends Drawable> unwrap(@NonNull Drawable drawable)

Unwrap drawable if it is the result of a call to wrap.

static @NonNull Drawable
wrap(@NonNull Drawable drawable)

Potentially wrap drawable so that it may be used for tinting across the different API levels, via the tinting methods in this class.

Public methods

applyTheme

Added in 1.1.0
public static void applyTheme(@NonNull Drawable drawable, @NonNull Resources.Theme theme)

Applies the specified theme to this Drawable and its children.

canApplyTheme

Added in 1.1.0
public static boolean canApplyTheme(@NonNull Drawable drawable)

Whether a theme can be applied to this Drawable and its children.

clearColorFilter

Added in 1.1.0
public static void clearColorFilter(@NonNull Drawable drawable)

Removes the color filter from the given drawable.

getAlpha

Added in 1.1.0
Deprecated in 1.15.0-rc01
@ReplaceWith(expression = "drawable.getAlpha()")
public static int getAlpha(@NonNull Drawable drawable)

Get the alpha value of the drawable. 0 means fully transparent, 255 means fully opaque.

Parameters
@NonNull Drawable drawable

The Drawable against which to invoke the method.

getColorFilter

Added in 1.1.0
public static @Nullable ColorFilter getColorFilter(@NonNull Drawable drawable)

Returns the current color filter, or null if none set.

Returns
@Nullable ColorFilter

the current color filter, or null if none set

getLayoutDirection

Added in 1.1.0
public static int getLayoutDirection(@NonNull Drawable drawable)

Returns the resolved layout direction for this Drawable.

inflate

Added in 1.1.0
public 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
@NonNull Drawable drawable

drawable to inflate.

@NonNull Resources res

Resources used to resolve attribute values

@NonNull XmlPullParser parser

XML parser from which to inflate this Drawable

@NonNull AttributeSet attrs

Base set of attribute values

@Nullable Resources.Theme theme

Theme to apply, may be null

isAutoMirrored

Added in 1.1.0
Deprecated in 1.15.0-rc01
@ReplaceWith(expression = "drawable.isAutoMirrored()")
public static boolean isAutoMirrored(@NonNull Drawable drawable)

Tells if this Drawable will be automatically mirrored when its layout direction is RTL right-to-left. See android.util.LayoutDirection.

If running on a pre-KITKAT device this method returns false.

Parameters
@NonNull Drawable drawable

The Drawable against which to invoke the method.

Returns
boolean

boolean Returns true if this Drawable will be automatically mirrored.

jumpToCurrentState

Added in 1.1.0
Deprecated in 1.1.0
@ReplaceWith(expression = "drawable.jumpToCurrentState()")
public static void jumpToCurrentState(@NonNull Drawable drawable)

Call Drawable.jumpToCurrentState().

Parameters
@NonNull Drawable drawable

The Drawable against which to invoke the method.

setAutoMirrored

Added in 1.1.0
Deprecated in 1.15.0-rc01
@ReplaceWith(expression = "drawable.setAutoMirrored(mirrored)")
public static void setAutoMirrored(@NonNull Drawable drawable, boolean mirrored)

Set whether this Drawable is automatically mirrored when its layout direction is RTL (right-to left). See android.util.LayoutDirection.

If running on a pre-KITKAT device this method does nothing.

Parameters
@NonNull Drawable drawable

The Drawable against which to invoke the method.

boolean mirrored

Set to true if the Drawable should be mirrored, false if not.

setHotspot

Added in 1.1.0
public static void setHotspot(@NonNull Drawable drawable, float x, float y)

Specifies the hotspot's location within the drawable.

Parameters
@NonNull Drawable drawable

The Drawable against which to invoke the method.

float x

The X coordinate of the center of the hotspot

float y

The Y coordinate of the center of the hotspot

setHotspotBounds

Added in 1.1.0
public 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 be different from the drawable bounds.

Parameters
@NonNull Drawable drawable

The Drawable against which to invoke the method.

int left

position in pixels of the left bound

int top

position in pixels of the top bound

int right

position in pixels of the right bound

int bottom

position in pixels of the bottom bound

setLayoutDirection

Added in 1.1.0
public static boolean setLayoutDirection(@NonNull Drawable drawable, int layoutDirection)

Set the layout direction for this drawable. Should be a resolved layout direction, as the Drawable has no capacity to do the resolution on its own.

Parameters
@NonNull Drawable drawable

drawable for which to set the layout direction.

int layoutDirection

the resolved layout direction for the drawable, either LAYOUT_DIRECTION_LTR or LAYOUT_DIRECTION_RTL

Returns
boolean

true if the layout direction change has caused the appearance of the drawable to change such that it needs to be re-drawn, false otherwise

setTint

Added in 1.1.0
public static void setTint(@NonNull Drawable drawable, @ColorInt int tint)

Specifies a tint for drawable.

Parameters
@NonNull Drawable drawable

The Drawable against which to invoke the method.

@ColorInt int tint

Color to use for tinting this drawable

setTintList

Added in 1.1.0
public static void setTintList(@NonNull Drawable drawable, @Nullable ColorStateList tint)

Specifies a tint for drawable as a color state list.

Parameters
@NonNull Drawable drawable

The Drawable against which to invoke the method.

@Nullable ColorStateList tint

Color state list to use for tinting this drawable, or null to clear the tint

setTintMode

Added in 1.1.0
public static void setTintMode(@NonNull Drawable drawable, @Nullable PorterDuff.Mode tintMode)

Specifies a tint blending mode for drawable.

Parameters
@NonNull Drawable drawable

The Drawable against which to invoke the method.

@Nullable PorterDuff.Mode tintMode

A Porter-Duff blending mode

unwrap

Added in 1.1.0
public static T <T extends Drawable> unwrap(@NonNull Drawable drawable)

Unwrap drawable if it is the result of a call to wrap. If the drawable is not the result of a call to wrap then drawable is returned as-is.

Parameters
@NonNull Drawable drawable

The drawable to unwrap

Returns
T

the unwrapped Drawable or drawable if it hasn't been wrapped.

See also
wrap

wrap

Added in 1.1.0
public static @NonNull Drawable wrap(@NonNull Drawable drawable)

Potentially wrap drawable so that it may be used for tinting across the different API levels, via the tinting methods in this class.

If the given drawable is wrapped, we will copy over certain state over to the wrapped drawable, 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 with the 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 android.graphics.drawable.Drawable again, you can use the value returned from unwrap.

Parameters
@NonNull Drawable drawable

The Drawable to process

Returns
@NonNull Drawable

A drawable capable of being tinted across all API levels.