public class TintedDrawable
extends android.graphics.drawable.StateListDrawable
TintedDrawable implements support for color tinting introduced in Android API 21.
It takes another drawable as a 'pattern' for tinting and a tint color or
a ColorStateList for state-based tinting.
You should provide pattern drawable and tint color or ColorStateList through constructor arguments. You can also customize tint mode by calling a
setTintMode(android.graphics.PorterDuff.Mode) method. The default mode is the most
commonly used PorterDuff.Mode.SRC_IN mode.
This implementation tints drawable resource by changing a pattern's color filter based on the
state of TintedDrawable instance. It stores only one copy of the pattern drawable
so does not consume a significant amount of additional memory.
Feel free to use TintedDrawable for both API 21 and lower. This implementation
will use API 21 if running on Lollipop devices or higher and fall back to custom implementation
on the lower API versions.
PorterDuff.Mode| Modifier and Type | Field and Description |
|---|---|
static int |
NO_TINT
This color means 'no tint', e.g. the source drawable will be shown as is.
|
| Constructor and Description |
|---|
TintedDrawable(android.graphics.drawable.Drawable patternDrawable,
android.content.res.ColorStateList colorStateList) |
TintedDrawable(android.graphics.drawable.Drawable patternDrawable,
int tint) |
| Modifier and Type | Method and Description |
|---|---|
void |
applyTheme(android.content.res.Resources.Theme t)
Applies the specified theme to this Drawable and its children.
|
boolean |
canApplyTheme() |
void |
draw(android.graphics.Canvas canvas)
Draw in its bounds (set via setBounds) respecting optional effects such
as alpha (set via setAlpha) and color filter (set via setColorFilter).
|
int |
getAlpha()
Gets the current alpha value for the drawable. 0 means fully transparent,
255 means fully opaque.
|
android.graphics.ColorFilter |
getColorFilter()
Returns the current color filter, or
null if none set. |
android.graphics.drawable.Drawable.ConstantState |
getConstantState()
Return a
Drawable.ConstantState instance that holds the shared state of this Drawable. |
int |
getIntrinsicHeight()
Return the intrinsic height of the underlying drawable object.
|
int |
getIntrinsicWidth()
Return the intrinsic width of the underlying drawable object.
|
int |
getMinimumHeight()
Returns the minimum height suggested by this Drawable.
|
int |
getMinimumWidth()
Returns the minimum width suggested by this Drawable.
|
int |
getOpacity()
Return the opacity/transparency of this Drawable.
|
void |
getOutline(android.graphics.Outline outline)
Called to get the drawable to populate the Outline that defines its drawing area.
|
boolean |
getPadding(android.graphics.Rect padding)
Return in padding the insets suggested by this Drawable for placing
content inside the drawable's bounds.
|
android.graphics.drawable.Drawable |
getPatternDrawable()
Returns a pattern drawable
|
android.graphics.Region |
getTransparentRegion()
Returns a Region representing the part of the Drawable that is completely
transparent.
|
void |
invalidateSelf()
Use the current
Drawable.Callback implementation to have this Drawable
redrawn. |
boolean |
isAutoMirrored()
Tells if this Drawable will be automatically mirrored when its layout direction is RTL
right-to-left.
|
boolean |
isStateful()
Indicates whether this drawable will change its appearance based on
state.
|
void |
jumpToCurrentState()
If this Drawable does transition animations between states, ask that
it immediately jump to the current state and skip any active animations.
|
android.graphics.drawable.Drawable |
mutate()
Make this drawable mutable.
|
protected void |
onBoundsChange(android.graphics.Rect bounds)
Override this in your subclass to change appearance if you vary based on
the bounds.
|
protected boolean |
onLevelChange(int level)
Override this in your subclass to change appearance if you vary based
on level.
|
protected boolean |
onStateChange(int[] state)
Override this in your subclass to change appearance if you recognize the
specified state.
|
void |
scheduleSelf(Runnable what,
long when)
Use the current
Drawable.Callback implementation to have this Drawable
scheduled. |
void |
setAlpha(int alpha)
Specify an alpha value for the drawable. 0 means fully transparent, and
255 means fully opaque.
|
void |
setAutoMirrored(boolean mirrored)
Set whether this Drawable is automatically mirrored when its layout direction is RTL
(right-to left).
|
void |
setChangingConfigurations(int configs)
Set a mask of the configuration parameters for which this drawable
may change, requiring that it be re-created.
|
void |
setColorFilter(android.graphics.ColorFilter cf)
Specify an optional color filter for the drawable.
|
void |
setDither(boolean dither)
Set to true to have the drawable dither its colors when drawn to a device
with fewer than 8-bits per color component.
|
void |
setFilterBitmap(boolean filter)
Set to true to have the drawable filter its bitmap when scaled or rotated
(for drawables that use bitmaps).
|
void |
setHotspot(float x,
float y)
Specifies the hotspot's location within the drawable.
|
void |
setHotspotBounds(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.
|
void |
setTint(int tint)
Specifies a tint for this drawable.
|
void |
setTintList(android.content.res.ColorStateList tint)
Specifies a tint for this drawable as a color state list.
|
void |
setTintMode(android.graphics.PorterDuff.Mode tintMode)
Specifies a tint blending mode for this drawable.
|
boolean |
setVisible(boolean visible,
boolean restart)
Set whether this Drawable is visible.
|
void |
unscheduleSelf(Runnable what)
Use the current
Drawable.Callback implementation to have this Drawable
unscheduled. |
getChangingConfigurations, getCurrent, invalidateDrawable, scheduleDrawable, selectDrawable, setConstantState, setEnterFadeDuration, setExitFadeDuration, unscheduleDrawableclearColorFilter, copyBounds, copyBounds, createFromPath, createFromResourceStream, createFromResourceStream, createFromStream, createFromXml, createFromXml, createFromXmlInner, createFromXmlInner, getBounds, getCallback, getDirtyBounds, getLevel, getState, inflate, isVisible, resolveOpacity, setBounds, setBounds, setCallback, setColorFilter, setLevel, setStatepublic static final int NO_TINT
public TintedDrawable(android.graphics.drawable.Drawable patternDrawable,
android.content.res.ColorStateList colorStateList)
public TintedDrawable(android.graphics.drawable.Drawable patternDrawable,
int tint)
public void draw(android.graphics.Canvas canvas)
draw in class android.graphics.drawable.DrawableContainercanvas - The canvas to draw intopublic void setChangingConfigurations(int configs)
setChangingConfigurations in class android.graphics.drawable.Drawableconfigs - A mask of the changing configuration parameters, as
defined by android.content.pm.ActivityInfo.android.content.pm.ActivityInfopublic void setDither(boolean dither)
setDither in class android.graphics.drawable.DrawableContainerdither - public void setFilterBitmap(boolean filter)
setFilterBitmap in class android.graphics.drawable.Drawablefilter - public void invalidateSelf()
Drawable.Callback implementation to have this Drawable
redrawn. Does nothing if there is no Callback attached to the
Drawable.invalidateSelf in class android.graphics.drawable.DrawableDrawable.Callback.invalidateDrawable(android.graphics.drawable.Drawable),
Drawable.getCallback(),
Drawable.setCallback(android.graphics.drawable.Drawable.Callback)public void scheduleSelf(Runnable what, long when)
Drawable.Callback implementation to have this Drawable
scheduled. Does nothing if there is no Callback attached to the
Drawable.scheduleSelf in class android.graphics.drawable.Drawablewhat - The action being scheduled.when - The time (in milliseconds) to run.Drawable.Callback.scheduleDrawable(android.graphics.drawable.Drawable, java.lang.Runnable, long)public void unscheduleSelf(Runnable what)
Drawable.Callback implementation to have this Drawable
unscheduled. Does nothing if there is no Callback attached to the
Drawable.unscheduleSelf in class android.graphics.drawable.Drawablewhat - The runnable that you no longer want called.Drawable.Callback.unscheduleDrawable(android.graphics.drawable.Drawable, java.lang.Runnable)public void setAlpha(int alpha)
setAlpha in class android.graphics.drawable.DrawableContaineralpha - public int getAlpha()
getAlpha in class android.graphics.drawable.DrawableContainerpublic void setColorFilter(android.graphics.ColorFilter cf)
null to
remove any existing color filter.setColorFilter in class android.graphics.drawable.DrawableContainercf - the color filter to apply, or null to remove the
existing color filterpublic void setTint(int tint)
setColorFilter(android.graphics.ColorFilter) overrides
tint.setTint in class android.graphics.drawable.Drawabletint - Color to use for tinting this drawablesetTintMode(android.graphics.PorterDuff.Mode)public void setTintList(android.content.res.ColorStateList tint)
setColorFilter(android.graphics.ColorFilter) overrides
tint.setTintList in class android.graphics.drawable.DrawableContainertint - Color state list to use for tinting this drawable, or null to
clear the tintsetTintMode(android.graphics.PorterDuff.Mode)public void setTintMode(android.graphics.PorterDuff.Mode tintMode)
setColorFilter(android.graphics.ColorFilter) overrides
tint.setTintMode in class android.graphics.drawable.DrawableContainertintMode - Color state list to use for tinting this drawable, or null to
clear the tintpublic android.graphics.ColorFilter getColorFilter()
null if none set.getColorFilter in class android.graphics.drawable.Drawablenull if none setpublic void setHotspot(float x,
float y)
setHotspot in class android.graphics.drawable.DrawableContainerx - The X coordinate of the center of the hotspoty - The Y coordinate of the center of the hotspotpublic void setHotspotBounds(int left,
int top,
int right,
int bottom)
setHotspotBounds in class android.graphics.drawable.DrawableContainerleft - top - right - bottom - public boolean isStateful()
isStateful in class android.graphics.drawable.StateListDrawableDrawable.setState(int[])public void jumpToCurrentState()
jumpToCurrentState in class android.graphics.drawable.DrawableContainerpublic boolean setVisible(boolean visible,
boolean restart)
setVisible in class android.graphics.drawable.DrawableContainervisible - Set to true if visible, false if not.restart - You can supply true here to force the drawable to behave
as if it has just become visible, even if it had last
been set visible. Used for example to force animations
to restart.public void setAutoMirrored(boolean mirrored)
LayoutDirection.setAutoMirrored in class android.graphics.drawable.DrawableContainermirrored - Set to true if the Drawable should be mirrored, false if not.public boolean isAutoMirrored()
LayoutDirection.isAutoMirrored in class android.graphics.drawable.DrawableContainerpublic void applyTheme(android.content.res.Resources.Theme t)
applyTheme in class android.graphics.drawable.DrawableContainert - public boolean canApplyTheme()
canApplyTheme in class android.graphics.drawable.DrawableContainerpublic int getOpacity()
PixelFormat:
PixelFormat.UNKNOWN,
PixelFormat.TRANSLUCENT,
PixelFormat.TRANSPARENT, or
PixelFormat.OPAQUE.
Generally a Drawable should be as conservative as possible with the
value it returns. For example, if it contains multiple child drawables
and only shows one of them at a time, if only one of the children is
TRANSLUCENT and the others are OPAQUE then TRANSLUCENT should be
returned. You can use the method Drawable.resolveOpacity(int, int) to perform a
standard reduction of two opacities to the appropriate single output.
Note that the returned value does not take into account a
custom alpha or color filter that has been applied by the client through
the setAlpha(int) or setColorFilter(android.graphics.ColorFilter) methods.
getOpacity in class android.graphics.drawable.DrawableContainerPixelFormatpublic android.graphics.Region getTransparentRegion()
getTransparentRegion in class android.graphics.drawable.Drawableprotected boolean onStateChange(int[] state)
onStateChange in class android.graphics.drawable.StateListDrawablestate - protected boolean onLevelChange(int level)
onLevelChange in class android.graphics.drawable.DrawableContainerlevel - protected void onBoundsChange(android.graphics.Rect bounds)
onBoundsChange in class android.graphics.drawable.DrawableContainerbounds - public int getIntrinsicWidth()
getIntrinsicWidth in class android.graphics.drawable.DrawableContainerpublic int getIntrinsicHeight()
getIntrinsicHeight in class android.graphics.drawable.DrawableContainerpublic int getMinimumWidth()
getMinimumWidth in class android.graphics.drawable.DrawableContainerpublic int getMinimumHeight()
getMinimumHeight in class android.graphics.drawable.DrawableContainerpublic boolean getPadding(android.graphics.Rect padding)
getPadding in class android.graphics.drawable.DrawableContainerpadding - public void getOutline(android.graphics.Outline outline)
ViewOutlineProvider to define
the outline of the View.
The default behavior defines the outline to be the bounding rectangle of 0 alpha.
Subclasses that wish to convey a different shape or alpha value must override this method.getOutline in class android.graphics.drawable.DrawableContaineroutline - View.setOutlineProvider(android.view.ViewOutlineProvider)public android.graphics.drawable.Drawable mutate()
mutate in class android.graphics.drawable.StateListDrawableDrawable.ConstantState,
getConstantState()public android.graphics.drawable.Drawable.ConstantState getConstantState()
Drawable.ConstantState instance that holds the shared state of this Drawable.getConstantState in class android.graphics.drawable.DrawableContainerDrawable.ConstantState,
Drawable.mutate()public android.graphics.drawable.Drawable getPatternDrawable()
Copyright © 2015. All Rights Reserved.