Skip to content

Most visited

Recently visited

navigation

DrawableCompat

public final class DrawableCompat
extends Object

java.lang.Object
   ↳ android.support.v4.graphics.drawable.DrawableCompat


Helper for accessing features in Drawable introduced after API level 4 in a backwards compatible fashion.

Summary

Public methods

static void applyTheme(Drawable drawable, Resources.Theme t)

Applies the specified theme to this Drawable and its children.

static boolean canApplyTheme(Drawable drawable)

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

static int getAlpha(Drawable drawable)

Get the alpha value of the drawable.

static ColorFilter getColorFilter(Drawable drawable)

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

static int getLayoutDirection(Drawable drawable)

Returns the resolved layout direction for this Drawable.

static void inflate(Drawable drawable, Resources res, XmlPullParser parser, AttributeSet attrs, Resources.Theme theme)

Inflate this Drawable from an XML resource optionally styled by a theme.

static boolean isAutoMirrored(Drawable drawable)

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

static void jumpToCurrentState(Drawable drawable)

Call Drawable.jumpToCurrentState().

static void setAutoMirrored(Drawable drawable, boolean mirrored)

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

static void setHotspot(Drawable drawable, float x, float y)

Specifies the hotspot's location within the drawable.

static void setHotspotBounds(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(Drawable drawable, int layoutDirection)

Set the layout direction for this drawable.

static void setTint(Drawable drawable, int tint)

Specifies a tint for drawable.

static void setTintList(Drawable drawable, ColorStateList tint)

Specifies a tint for drawable as a color state list.

static void setTintMode(Drawable drawable, PorterDuff.Mode tintMode)

Specifies a tint blending mode for drawable.

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

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

static Drawable wrap(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.

Inherited methods

From class java.lang.Object

Public methods

applyTheme

void applyTheme (Drawable drawable, 
                Resources.Theme t)

Applies the specified theme to this Drawable and its children.

Parameters
drawable Drawable
t Resources.Theme

canApplyTheme

boolean canApplyTheme (Drawable drawable)

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

Parameters
drawable Drawable
Returns
boolean

getAlpha

int getAlpha (Drawable drawable)

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

Parameters
drawable Drawable: The Drawable against which to invoke the method.
Returns
int

getColorFilter

ColorFilter getColorFilter (Drawable drawable)

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

Parameters
drawable Drawable
Returns
ColorFilter the current color filter, or null if none set

getLayoutDirection

int getLayoutDirection (Drawable drawable)

Returns the resolved layout direction for this Drawable.

Parameters
drawable Drawable
Returns
int One of LAYOUT_DIRECTION_LTR, LAYOUT_DIRECTION_RTL

See also:

inflate

void inflate (Drawable drawable, 
                Resources res, 
                XmlPullParser parser, 
                AttributeSet attrs, 
                Resources.Theme theme)

Inflate this Drawable from an XML resource optionally styled by a theme.

Parameters
drawable Drawable
res Resources: Resources used to resolve attribute values
parser XmlPullParser: XML parser from which to inflate this Drawable
attrs AttributeSet: Base set of attribute values
theme Resources.Theme: Theme to apply, may be null
Throws
XmlPullParserException
IOException
XmlPullParserException

isAutoMirrored

boolean isAutoMirrored (Drawable drawable)

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

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

Parameters
drawable Drawable: The Drawable against which to invoke the method.
Returns
boolean boolean Returns true if this Drawable will be automatically mirrored.

jumpToCurrentState

void jumpToCurrentState (Drawable drawable)

Call Drawable.jumpToCurrentState().

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

Parameters
drawable Drawable: The Drawable against which to invoke the method.

setAutoMirrored

void setAutoMirrored (Drawable drawable, 
                boolean mirrored)

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

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

Parameters
drawable Drawable: The Drawable against which to invoke the method.
mirrored boolean: Set to true if the Drawable should be mirrored, false if not.

setHotspot

void setHotspot (Drawable drawable, 
                float x, 
                float y)

Specifies the hotspot's location within the drawable.

Parameters
drawable Drawable: The Drawable against which to invoke the method.
x float: The X coordinate of the center of the hotspot
y float: The Y coordinate of the center of the hotspot

setHotspotBounds

void setHotspotBounds (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
drawable Drawable: The Drawable against which to invoke the method.
left int
top int
right int
bottom int

setLayoutDirection

boolean setLayoutDirection (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
drawable Drawable
layoutDirection int: 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

See also:

setTint

void setTint (Drawable drawable, 
                int tint)

Specifies a tint for drawable.

Parameters
drawable Drawable: The Drawable against which to invoke the method.
tint int: Color to use for tinting this drawable

setTintList

void setTintList (Drawable drawable, 
                ColorStateList tint)

Specifies a tint for drawable as a color state list.

Parameters
drawable Drawable: The Drawable against which to invoke the method.
tint ColorStateList: Color state list to use for tinting this drawable, or null to clear the tint

setTintMode

void setTintMode (Drawable drawable, 
                PorterDuff.Mode tintMode)

Specifies a tint blending mode for drawable.

Parameters
drawable Drawable: The Drawable against which to invoke the method.
tintMode PorterDuff.Mode: A Porter-Duff blending mode

unwrap

T unwrap (Drawable drawable)

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

Parameters
drawable Drawable: The drawable to unwrap
Returns
T the unwrapped Drawable or drawable if it hasn't been wrapped.

See also:

wrap

Drawable wrap (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.

You must use the result of this call. If the given drawable is being used by a view (as it's 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 Drawable again, you can use the value returned from unwrap(Drawable).

Parameters
drawable Drawable: The Drawable to process
Returns
Drawable A drawable capable of being tinted across all API levels.

See also:

This site uses cookies to store your preferences for site-specific language and display options.

Hooray!

This class requires API level or higher

This doc is hidden because your selected API level for the documentation is . You can change the documentation API level with the selector above the left navigation.

For more information about specifying the API level your app requires, read Supporting Different Platform Versions.