public class AnimatedVectorDrawableCompat extends Drawable implements Animatable
AnimatedVectorDrawable
.
For older API version, this class uses ObjectAnimator
and
AnimatorSet
to animate the properties of a
VectorDrawableCompat
to create an animated drawable.
AnimatedVectorDrawableCompat are defined in the same XML format as AnimatedVectorDrawable
.
Drawable.Callback, Drawable.ConstantState
Modifier and Type | Method and Description |
---|---|
void |
applyTheme(Resources.Theme t)
Applies the specified theme to this Drawable and its children.
|
boolean |
canApplyTheme() |
void |
clearColorFilter()
Removes the color filter for this drawable.
|
static AnimatedVectorDrawableCompat |
create(Context context,
int resId)
Create a AnimatedVectorDrawableCompat object.
|
static AnimatedVectorDrawableCompat |
createFromXmlInner(Context context,
Resources r,
XmlPullParser parser,
AttributeSet attrs,
Resources.Theme theme)
Create a AnimatedVectorDrawableCompat from inside an XML document using an optional
Resources.Theme . |
void |
draw(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.
|
int |
getChangingConfigurations()
Return a mask of the configuration parameters for which this drawable
may change, requiring that it be re-created.
|
ColorFilter |
getColorFilter()
Returns the current color filter, or
null if none set. |
Drawable.ConstantState |
getConstantState()
Return a
Drawable.ConstantState instance that holds the shared state of this Drawable. |
Drawable |
getCurrent() |
int |
getIntrinsicHeight()
Returns the drawable's intrinsic height.
|
int |
getIntrinsicWidth()
Returns the drawable's intrinsic width.
|
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.
|
boolean |
getPadding(Rect padding)
Return in padding the insets suggested by this Drawable for placing
content inside the drawable's bounds.
|
int[] |
getState()
Describes the current state, as a union of primitve states, such as
android.R.attr#state_focused ,
android.R.attr#state_selected , etc. |
Region |
getTransparentRegion()
Returns a Region representing the part of the Drawable that is completely
transparent.
|
void |
inflate(Resources res,
XmlPullParser parser,
AttributeSet attrs)
Inflate this Drawable from an XML resource.
|
void |
inflate(Resources res,
XmlPullParser parser,
AttributeSet attrs,
Resources.Theme theme)
Inflate this Drawable from an XML resource optionally styled by a theme.
|
boolean |
isAutoMirrored()
Tells if this Drawable will be automatically mirrored when its layout direction is RTL
right-to-left.
|
boolean |
isRunning()
Indicates whether the animation is running.
|
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.
|
Drawable |
mutate()
mutate() will be effective only if the getConstantState() is returning non-null.
|
protected void |
onBoundsChange(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 |
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(ColorFilter colorFilter)
Specify an optional color filter for the drawable.
|
void |
setColorFilter(int color,
PorterDuff.Mode mode)
Specify a color and Porter-Duff mode to be the color filter for this
drawable.
|
void |
setFilterBitmap(boolean filter)
Set to true to have the drawable filter its bitmaps with bilinear
sampling when they are scaled or rotated.
|
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.
|
boolean |
setState(int[] stateSet)
Specify a set of states for the drawable.
|
void |
setTint(int tint)
Specifies tint color for this drawable.
|
void |
setTintList(ColorStateList tint)
Specifies tint color for this drawable as a color state list.
|
void |
setTintMode(PorterDuff.Mode tintMode)
Specifies a tint blending mode for this drawable.
|
boolean |
setVisible(boolean visible,
boolean restart)
Set whether this Drawable is visible.
|
void |
start()
Starts the drawable's animation.
|
void |
stop()
Stops the drawable's animation.
|
clearMutated, copyBounds, copyBounds, createFromPath, createFromResourceStream, createFromResourceStream, createFromStream, createFromXml, createFromXml, createFromXmlInner, createFromXmlInner, getBounds, getCallback, getDirtyBounds, getHotspotBounds, getLayoutDirection, getLevel, getOpticalInsets, getOutline, invalidateSelf, isFilterBitmap, isProjected, isVisible, onLayoutDirectionChanged, parseTintMode, resolveOpacity, scheduleSelf, setBounds, setBounds, setCallback, setDither, setLayoutDirection, setLevel, setXfermode, unscheduleSelf
public Drawable mutate()
mutate
in class Drawable
Drawable.ConstantState
,
Drawable.getConstantState()
@Nullable public static AnimatedVectorDrawableCompat create(@NonNull Context context, @DrawableRes int resId)
context
- the context for creating the animators.resId
- the resource ID for AnimatedVectorDrawableCompat object.public static AnimatedVectorDrawableCompat createFromXmlInner(Context context, Resources r, XmlPullParser parser, AttributeSet attrs, Resources.Theme theme) throws XmlPullParserException, IOException
Resources.Theme
. Called on a parser positioned at a tag in an XML
document, tries to create a Drawable from that tag. Returns null
if the tag is not a valid drawable.XmlPullParserException
IOException
public Drawable.ConstantState getConstantState()
Drawable.ConstantState
instance that holds the shared state of this Drawable.
Note that we don't support constant state when SDK < 24.
Make sure you check the return value before using it.getConstantState
in class Drawable
Drawable.ConstantState
,
Drawable.mutate()
public int getChangingConfigurations()
Drawable
Drawable.setChangingConfigurations(int)
or 0 by default. Subclasses
may extend this to or in the changing configurations of any other
drawables they hold.getChangingConfigurations
in class Drawable
ActivityInfo
.ActivityInfo
public void draw(Canvas canvas)
Drawable
protected void onBoundsChange(Rect bounds)
Drawable
protected boolean onStateChange(int[] state)
Drawable
onStateChange
in class Drawable
protected boolean onLevelChange(int level)
Drawable
public int getAlpha()
Drawable
public void setAlpha(int alpha)
Drawable
public void setColorFilter(ColorFilter colorFilter)
Drawable
If a Drawable has a ColorFilter, each output pixel of the Drawable's drawing contents will be modified by the color filter before it is blended onto the render target of a Canvas.
Pass null
to remove any existing color filter.
Note: Setting a non-null
color
filter disables tint
.
setColorFilter
in class Drawable
colorFilter
- The color filter to apply, or null
to remove the
existing color filterpublic void setTint(int tint)
Drawable
A Drawable's drawing content will be blended together with its tint
before it is drawn to the screen. This functions similarly to
Drawable.setColorFilter(int, PorterDuff.Mode)
.
To clear the tint, pass null
to
Drawable.setTintList(ColorStateList)
.
Note: Setting a color filter via
Drawable.setColorFilter(ColorFilter)
or
Drawable.setColorFilter(int, PorterDuff.Mode)
overrides tint.
setTint
in interface TintAwareDrawable
setTint
in class Drawable
tint
- Color to use for tinting this drawableDrawable.setTintList(ColorStateList)
,
Drawable.setTintMode(PorterDuff.Mode)
public void setTintList(ColorStateList tint)
Drawable
A Drawable's drawing content will be blended together with its tint
before it is drawn to the screen. This functions similarly to
Drawable.setColorFilter(int, PorterDuff.Mode)
.
Note: Setting a color filter via
Drawable.setColorFilter(ColorFilter)
or
Drawable.setColorFilter(int, PorterDuff.Mode)
overrides tint.
setTintList
in interface TintAwareDrawable
setTintList
in class Drawable
tint
- Color state list to use for tinting this drawable, or
null
to clear the tintDrawable.setTint(int)
,
Drawable.setTintMode(PorterDuff.Mode)
public void setTintMode(PorterDuff.Mode tintMode)
Drawable
Defines how this drawable's tint color should be blended into the drawable
before it is drawn to screen. Default tint mode is PorterDuff.Mode#SRC_IN
.
Note: Setting a color filter via
Drawable.setColorFilter(ColorFilter)
or
Drawable.setColorFilter(int, PorterDuff.Mode)
overrides tint.
setTintMode
in interface TintAwareDrawable
setTintMode
in class Drawable
tintMode
- A Porter-Duff blending modeDrawable.setTint(int)
,
Drawable.setTintList(ColorStateList)
public boolean setVisible(boolean visible, boolean restart)
Drawable
setVisible
in class Drawable
visible
- 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 boolean isStateful()
Drawable
isStateful
in class Drawable
Drawable.setState(int[])
public int getOpacity()
Drawable
PixelFormat
:
PixelFormat.UNKNOWN
,
PixelFormat.TRANSLUCENT
,
PixelFormat.TRANSPARENT
, or
PixelFormat.OPAQUE
.
An OPAQUE drawable is one that draws all all content within its bounds, completely covering anything behind the drawable. A TRANSPARENT drawable is one that draws nothing within its bounds, allowing everything behind it to show through. A TRANSLUCENT drawable is a drawable in any other state, where the drawable will draw some, but not all, of the content within its bounds and at least some content behind the drawable will be visible. If the visibility of the drawable's contents cannot be determined, the safest/best return value is TRANSLUCENT.
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 necessarily take into account a
custom alpha or color filter that has been applied by the client through
the Drawable.setAlpha(int)
or Drawable.setColorFilter(android.graphics.ColorFilter)
methods. Some subclasses,
such as BitmapDrawable
, ColorDrawable
, and GradientDrawable
,
do account for the value of Drawable.setAlpha(int)
, but the general behavior is dependent
upon the implementation of the subclass.
getOpacity
in class Drawable
PixelFormat
public int getIntrinsicWidth()
Drawable
Intrinsic width is the width at which the drawable would like to be laid out, including any inherent padding. If the drawable has no intrinsic width, such as a solid color, this method returns -1.
getIntrinsicWidth
in class Drawable
public int getIntrinsicHeight()
Drawable
Intrinsic height is the height at which the drawable would like to be laid out, including any inherent padding. If the drawable has no intrinsic height, such as a solid color, this method returns -1.
getIntrinsicHeight
in class Drawable
public boolean isAutoMirrored()
Drawable
LayoutDirection
.isAutoMirrored
in class Drawable
public void setAutoMirrored(boolean mirrored)
Drawable
LayoutDirection
.setAutoMirrored
in class Drawable
mirrored
- Set to true if the Drawable should be mirrored, false if not.public void inflate(Resources res, XmlPullParser parser, AttributeSet attrs, Resources.Theme theme) throws XmlPullParserException, IOException
Drawable
inflate
in class Drawable
res
- Resources used to resolve attribute valuesparser
- XML parser from which to inflate this Drawableattrs
- Base set of attribute valuestheme
- Theme to apply, may be nullXmlPullParserException
IOException
public void inflate(Resources res, XmlPullParser parser, AttributeSet attrs) throws XmlPullParserException, IOException
Drawable
inflate
in class Drawable
XmlPullParserException
IOException
#inflate(Resources, XmlPullParser, AttributeSet, Theme)
public void applyTheme(Resources.Theme t)
Drawable
t
- the theme to applypublic boolean canApplyTheme()
canApplyTheme
in class Drawable
public boolean isRunning()
Animatable
isRunning
in interface Animatable
public void start()
Animatable
start
in interface Animatable
public void stop()
Animatable
stop
in interface Animatable
public void setColorFilter(int color, PorterDuff.Mode mode)
Drawable
Convenience for Drawable.setColorFilter(ColorFilter)
which constructs a
PorterDuffColorFilter
.
Note: Setting a color filter disables
tint
.
setColorFilter
in class Drawable
public ColorFilter getColorFilter()
Drawable
null
if none set.getColorFilter
in class Drawable
null
if none setpublic void setHotspot(float x, float y)
Drawable
setHotspot
in class Drawable
x
- 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)
Drawable
setHotspotBounds
in class Drawable
left
- position in pixels of the left boundtop
- position in pixels of the top boundright
- position in pixels of the right boundbottom
- position in pixels of the bottom boundDrawable.getHotspotBounds(android.graphics.Rect)
public void setFilterBitmap(boolean filter)
Drawable
This can improve appearance when bitmaps are rotated. If the drawable does not use bitmaps, this call is ignored.
setFilterBitmap
in class Drawable
Drawable.isFilterBitmap()
,
Paint.setFilterBitmap(boolean);
public void jumpToCurrentState()
Drawable
jumpToCurrentState
in class Drawable
public void clearColorFilter()
Drawable
clearColorFilter
in class Drawable
public Drawable getCurrent()
getCurrent
in class Drawable
StateListDrawable
and LevelListDrawable
this will be the child drawable
currently in use.public int getMinimumWidth()
Drawable
getMinimumWidth
in class Drawable
public int getMinimumHeight()
Drawable
getMinimumHeight
in class Drawable
public boolean getPadding(Rect padding)
Drawable
getPadding
in class Drawable
public int[] getState()
Drawable
android.R.attr#state_focused
,
android.R.attr#state_selected
, etc.
Some drawables may modify their imagery based on the selected state.public Region getTransparentRegion()
Drawable
getTransparentRegion
in class Drawable
public void setChangingConfigurations(int configs)
Drawable
setChangingConfigurations
in class Drawable
configs
- A mask of the changing configuration parameters, as
defined by ActivityInfo
.ActivityInfo
public boolean setState(int[] stateSet)
Drawable
android.R.attr#state_focused
,
android.R.attr#state_pressed
].
If the new state you are supplying causes the appearance of the
Drawable to change, then it is responsible for calling
Drawable.invalidateSelf()
in order to have itself redrawn, and
true will be returned from this function.
Note: The Drawable holds a reference on to stateSet until a new state array is given to it, so you must not modify this array during that time.