DrawableCompat
public final class DrawableCompat
Helper for accessing features in android.graphics.drawable.Drawable.
Summary
Public methods |
|
|---|---|
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 void |
clearColorFilter(@NonNull Drawable drawable)Removes the color filter from the given drawable. |
static int |
@ReplaceWith(expression = "drawable.getAlpha()")This method is deprecated. Call |
static @Nullable ColorFilter |
getColorFilter(@NonNull Drawable drawable)Returns the current color filter, or |
static int |
getLayoutDirection(@NonNull Drawable drawable)Returns the resolved layout direction for this Drawable. |
static void |
inflate(Inflate this Drawable from an XML resource optionally styled by a theme. |
static boolean |
@ReplaceWith(expression = "drawable.isAutoMirrored()")This method is deprecated. Call |
static void |
@ReplaceWith(expression = "drawable.jumpToCurrentState()")This method is deprecated. Use |
static void |
@ReplaceWith(expression = "drawable.setAutoMirrored(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(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 |
Specifies a tint for |
static void |
setTintList(@NonNull Drawable drawable, @Nullable ColorStateList tint)Specifies a tint for |
static void |
setTintMode(@NonNull Drawable drawable, @Nullable PorterDuff.Mode tintMode)Specifies a tint blending mode for |
static T |
Unwrap |
static @NonNull Drawable |
Potentially wrap |
Public methods
applyTheme
public static void applyTheme(@NonNull Drawable drawable, @NonNull Resources.Theme theme)
Applies the specified theme to this Drawable and its children.
canApplyTheme
public static boolean canApplyTheme(@NonNull Drawable drawable)
Whether a theme can be applied to this Drawable and its children.
clearColorFilter
public static void clearColorFilter(@NonNull Drawable drawable)
Removes the color filter from the given drawable.
@ReplaceWith(expression = "drawable.getAlpha()")
public static intgetAlpha(@NonNull Drawable drawable)
Get the alpha value of the drawable. 0 means fully transparent, 255 means fully opaque.
getColorFilter
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 |
getLayoutDirection
public static int getLayoutDirection(@NonNull Drawable drawable)
Returns the resolved layout direction for this Drawable.
| Returns | |
|---|---|
int |
| See also | |
|---|---|
setLayoutDirection |
inflate
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 |
@ReplaceWith(expression = "drawable.isAutoMirrored()")
public static booleanisAutoMirrored(@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.
| Returns | |
|---|---|
boolean |
boolean Returns true if this Drawable will be automatically mirrored. |
@ReplaceWith(expression = "drawable.jumpToCurrentState()")
public static voidjumpToCurrentState(@NonNull Drawable drawable)
@ReplaceWith(expression = "drawable.setAutoMirrored(mirrored)")
public static voidsetAutoMirrored(@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.
setHotspot
public static void setHotspot(@NonNull Drawable drawable, float x, float y)
Specifies the hotspot's location within the drawable.
setHotspotBounds
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.
setLayoutDirection
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 |
| Returns | |
|---|---|
boolean |
|
| See also | |
|---|---|
getLayoutDirection |
setTint
public static void setTint(@NonNull Drawable drawable, @ColorInt int tint)
Specifies a tint for drawable.
setTintList
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
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
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.
| Returns | |
|---|---|
T |
the unwrapped |
| See also | |
|---|---|
wrap |
wrap
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.
| See also | |
|---|---|
setTint |
|
setTintList |
|
setTintMode |
|
unwrap |