DrawableCompat

Added in 1.1.0

class DrawableCompat


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

Summary

Public functions

java-static Unit
applyTheme(drawable: Drawable, theme: Resources.Theme)

Applies the specified theme to this Drawable and its children.

java-static Boolean

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

java-static Unit

Removes the color filter from the given drawable.

java-static Int
getAlpha(drawable: Drawable)

Get the alpha value of the drawable.

java-static ColorFilter?

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

java-static Int

Returns the resolved layout direction for this Drawable.

java-static Unit
inflate(
    drawable: Drawable,
    res: Resources,
    parser: XmlPullParser,
    attrs: AttributeSet,
    theme: Resources.Theme?
)

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

java-static Boolean

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

java-static Unit

This function is deprecated.

Use jumpToCurrentState directly.

java-static Unit
setAutoMirrored(drawable: Drawable, mirrored: Boolean)

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

java-static Unit
setHotspot(drawable: Drawable, x: Float, y: Float)

Specifies the hotspot's location within the drawable.

java-static Unit
setHotspotBounds(
    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.

java-static Boolean
setLayoutDirection(drawable: Drawable, layoutDirection: Int)

Set the layout direction for this drawable.

java-static Unit
setTint(drawable: Drawable, tint: @ColorInt Int)

Specifies a tint for drawable.

java-static Unit
setTintList(drawable: Drawable, tint: ColorStateList?)

Specifies a tint for drawable as a color state list.

java-static Unit
setTintMode(drawable: Drawable, tintMode: PorterDuff.Mode?)

Specifies a tint blending mode for drawable.

java-static T!
<T : Drawable?> unwrap(drawable: Drawable)

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

java-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.

Public functions

applyTheme

Added in 1.1.0
java-static fun applyTheme(drawable: Drawable, theme: Resources.Theme): Unit

Applies the specified theme to this Drawable and its children.

canApplyTheme

Added in 1.1.0
java-static fun canApplyTheme(drawable: Drawable): Boolean

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

clearColorFilter

Added in 1.1.0
java-static fun clearColorFilter(drawable: Drawable): Unit

Removes the color filter from the given drawable.

getAlpha

Added in 1.1.0
java-static fun getAlpha(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

Added in 1.1.0
java-static fun getColorFilter(drawable: Drawable): ColorFilter?

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

Returns
ColorFilter?

the current color filter, or null if none set

getLayoutDirection

Added in 1.1.0
java-static fun getLayoutDirection(drawable: Drawable): Int

Returns the resolved layout direction for this Drawable.

inflate

Added in 1.1.0
java-static fun inflate(
    drawable: Drawable,
    res: Resources,
    parser: XmlPullParser,
    attrs: AttributeSet,
    theme: Resources.Theme?
): Unit

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

Parameters
drawable: Drawable

drawable to inflate.

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

isAutoMirrored

Added in 1.1.0
java-static fun isAutoMirrored(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-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

Added in 1.1.0
Deprecated in 1.1.0
java-static fun jumpToCurrentState(drawable: Drawable): Unit

Call Drawable.jumpToCurrentState().

Parameters
drawable: Drawable

The Drawable against which to invoke the method.

setAutoMirrored

Added in 1.1.0
java-static fun setAutoMirrored(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-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

Added in 1.1.0
java-static fun setHotspot(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: Float

The X coordinate of the center of the hotspot

y: Float

The Y coordinate of the center of the hotspot

setHotspotBounds

Added in 1.1.0
java-static fun setHotspotBounds(
    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.

left: Int

position in pixels of the left bound

top: Int

position in pixels of the top bound

right: Int

position in pixels of the right bound

bottom: Int

position in pixels of the bottom bound

setLayoutDirection

Added in 1.1.0
java-static fun setLayoutDirection(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
drawable: Drawable

drawable for which to set the layout direction.

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

setTint

Added in 1.1.0
java-static fun setTint(drawable: Drawable, tint: @ColorInt Int): Unit

Specifies a tint for drawable.

Parameters
drawable: Drawable

The Drawable against which to invoke the method.

tint: @ColorInt Int

Color to use for tinting this drawable

setTintList

Added in 1.1.0
java-static fun setTintList(drawable: Drawable, 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: ColorStateList?

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

setTintMode

Added in 1.1.0
java-static fun setTintMode(drawable: Drawable, tintMode: PorterDuff.Mode?): Unit

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

Added in 1.1.0
java-static fun <T : Drawable?> unwrap(drawable: Drawable): T!

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

Parameters
drawable: Drawable

The Drawable to process

Returns
Drawable

A drawable capable of being tinted across all API levels.