Register now for Android Dev Summit 2019!

DrawableCompat

class DrawableCompat
kotlin.Any
   ↳ androidx.core.graphics.drawable.DrawableCompat

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

Summary

Public methods

static Unit
applyTheme(@NonNull drawable: Drawable, @NonNull 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 Unit
clearColorFilter(@NonNull drawable: Drawable)

Removes the color filter from the given drawable.

static Int
getAlpha(@NonNull drawable: Drawable)

Get the alpha value of the drawable.

static ColorFilter!
getColorFilter(@NonNull drawable: Drawable)

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

static Int
getLayoutDirection(@NonNull drawable: Drawable)

Returns the resolved layout direction for this Drawable.

static Unit
inflate(@NonNull drawable: Drawable, @NonNull res: Resources, @NonNull parser: XmlPullParser, @NonNull attrs: AttributeSet, @Nullable theme: Theme?)

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

static Boolean
isAutoMirrored(@NonNull drawable: Drawable)

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

static Unit
jumpToCurrentState(@NonNull drawable: Drawable)

Call Drawable.jumpToCurrentState().

static Unit
setAutoMirrored(@NonNull drawable: Drawable, mirrored: Boolean)

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

static Unit
setHotspot(@NonNull drawable: Drawable, x: Float, y: Float)

Specifies the hotspot's location within the drawable.

static Unit
setHotspotBounds(@NonNull drawable: Drawable, left: Int, top: Int, right: Int, bottom: Int)

Sets the bounds to which the hotspot is constrained, if they should be different from the drawable bounds.

static Boolean
setLayoutDirection(@NonNull drawable: Drawable, layoutDirection: Int)

Set the layout direction for this drawable.

static Unit
setTint(@NonNull drawable: Drawable, tint: Int)

Specifies a tint for drawable.

static Unit
setTintList(@NonNull drawable: Drawable, @Nullable tint: ColorStateList?)

Specifies a tint for drawable as a color state list.

static Unit
setTintMode(@NonNull drawable: Drawable, @NonNull tintMode: Mode)

Specifies a tint blending mode for drawable.

static T
unwrap(@NonNull drawable: Drawable)

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

static 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

static fun applyTheme(@NonNull drawable: Drawable, @NonNull theme: Theme): Unit

Applies the specified theme to this Drawable and its children.

canApplyTheme

static fun canApplyTheme(@NonNull drawable: Drawable): Boolean

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

clearColorFilter

static fun clearColorFilter(@NonNull drawable: Drawable): Unit

Removes the color filter from the given drawable.

getAlpha

static fun getAlpha(@NonNull drawable: Drawable): Int

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.

getColorFilter

static fun getColorFilter(@NonNull drawable: Drawable): ColorFilter!

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

Return
ColorFilter!: the current color filter, or null if none set

getLayoutDirection

static fun getLayoutDirection(@NonNull drawable: Drawable): Int

Returns the resolved layout direction for this Drawable.

Return
Int: One of ViewCompat#LAYOUT_DIRECTION_LTR, ViewCompat#LAYOUT_DIRECTION_RTL

inflate

static fun inflate(@NonNull drawable: Drawable, @NonNull res: Resources, @NonNull parser: XmlPullParser, @NonNull attrs: AttributeSet, @Nullable theme: Theme?): Unit

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

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

isAutoMirrored

static fun isAutoMirrored(@NonNull drawable: Drawable): Boolean

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-android.os.Build.VERSION_CODES#KITKAT device this method returns false.

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

jumpToCurrentState

static fun jumpToCurrentState(@NonNull drawable: Drawable): Unit

Deprecated: Use Drawable#jumpToCurrentState() directly.

Call Drawable.jumpToCurrentState().

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

setAutoMirrored

static fun setAutoMirrored(@NonNull drawable: Drawable, mirrored: Boolean): Unit

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-android.os.Build.VERSION_CODES#KITKAT device this method does nothing.

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

setHotspot

static fun setHotspot(@NonNull drawable: Drawable, x: Float, y: Float): Unit

Specifies the hotspot's location within the drawable.

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

setHotspotBounds

static fun setHotspotBounds(@NonNull drawable: Drawable, left: Int, top: Int, right: Int, bottom: Int): Unit

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.

setLayoutDirection

static fun setLayoutDirection(@NonNull drawable: Drawable, layoutDirection: Int): Boolean

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
layoutDirection Drawable: the resolved layout direction for the drawable, either ViewCompat#LAYOUT_DIRECTION_LTR or ViewCompat#LAYOUT_DIRECTION_RTL
Return
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

static fun setTint(@NonNull drawable: Drawable, tint: Int): Unit

Specifies a tint for drawable.

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

setTintList

static fun setTintList(@NonNull drawable: Drawable, @Nullable tint: ColorStateList?): Unit

Specifies a tint for drawable as a color state list.

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

setTintMode

static fun setTintMode(@NonNull drawable: Drawable, @NonNull tintMode: Mode): Unit

Specifies a tint blending mode for drawable.

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

unwrap

static fun <T : Drawable!> unwrap(@NonNull drawable: Drawable): T

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
Return
T: the unwrapped Drawable or drawable if it hasn't been wrapped.

See Also

wrap

static fun wrap(@NonNull drawable: 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(Drawable).

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