public class AnimatedVectorDrawable extends Drawable implements Animatable2
VectorDrawable
with
animations defined using ObjectAnimator
or
AnimatorSet
.
Starting from API 25, AnimatedVectorDrawable runs on RenderThread (as opposed to on UI thread for
earlier APIs). This means animations in AnimatedVectorDrawable can remain smooth even when there
is heavy workload on the UI thread. Note: If the UI thread is unresponsive, RenderThread may
continue animating until the UI thread is capable of pushing another frame. Therefore, it is not
possible to precisely coordinate a RenderThread-enabled AnimatedVectorDrawable with UI thread
animations. Additionally,
Animatable2.AnimationCallback.onAnimationEnd(Drawable)
will be
called the frame after the AnimatedVectorDrawable finishes on the RenderThread.
AnimatedVectorDrawable can be defined in either three separate XML files, or one XML.
Animations can be performed on both group and path attributes, which requires groups and paths to have unique names in the same VectorDrawable. Groups and paths without animations do not need to be named.
Below is an example of a VectorDrawable defined in vectordrawable.xml. This VectorDrawable is referred to by its file name (not including file suffix) in the AnimatedVectorDrawable XML example.<vector xmlns:android="http://schemas.android.com/apk/res/android" android:height="64dp" android:width="64dp" android:viewportHeight="600" android:viewportWidth="600" > <group android:name="rotationGroup" android:pivotX="300.0" android:pivotY="300.0" android:rotation="45.0" > <path android:name="v" android:fillColor="#000000" android:pathData="M300,70 l 0,-70 70,70 0,0 -70,70z" /> </group> </vector>
An AnimatedVectorDrawable element has a VectorDrawable attribute, and one or more target element(s). The target elements can be the path or group to be animated. Each target element contains a name attribute that references a property (of a path or a group) to animate, and an animation attribute that points to an ObjectAnimator or an AnimatorSet.
The following code sample defines an AnimatedVectorDrawable. Note that the names refer to the groups and paths in the VectorDrawable XML above.<animated-vector xmlns:android="http://schemas.android.com/apk/res/android" android:drawable="@drawable/vectordrawable" > <target android:name="rotationGroup" android:animation="@anim/rotation" /> <target android:name="v" android:animation="@anim/path_morph" /> </animated-vector>
From the previous example of AnimatedVectorDrawable, two animations were used: rotation.xml and path_morph.xml.
rotation.xml rotates the target group from 0 degree to 360 degrees over 6000ms:<objectAnimator android:duration="6000" android:propertyName="rotation" android:valueFrom="0" android:valueTo="360" />path_morph.xml morphs the path from one shape into the other. Note that the paths must be compatible for morphing. Specifically, the paths must have the same commands, in the same order, and must have the same number of parameters for each command. It is recommended to store path strings as string resources for reuse.
<set xmlns:android="http://schemas.android.com/apk/res/android"> <objectAnimator android:duration="3000" android:propertyName="pathData" android:valueFrom="M300,70 l 0,-70 70,70 0,0 -70,70z" android:valueTo="M300,70 l 0,-70 70,0 0,140 -70,0 z" android:valueType="pathType"/> </set>
Since the AAPT tool supports a new format that bundles several related XML files together, we can merge the XML files from the previous examples into one XML file:
<animated-vector xmlns:android="http://schemas.android.com/apk/res/android" > <aapt:attr name="android:drawable"> <vector android:height="64dp" android:width="64dp" android:viewportHeight="600" android:viewportWidth="600" > <group android:name="rotationGroup" android:pivotX="300.0" android:pivotY="300.0" android:rotation="45.0" > <path android:name="v" android:fillColor="#000000" android:pathData="M300,70 l 0,-70 70,70 0,0 -70,70z" /> </group> </vector> </aapt:attr> <target android:name="rotationGroup"> * <aapt:attr name="android:animation"> <objectAnimator android:duration="6000" android:propertyName="rotation" android:valueFrom="0" android:valueTo="360" /> </aapt:attr> </target> <target android:name="v" > <aapt:attr name="android:animation"> <set> <objectAnimator android:duration="3000" android:propertyName="pathData" android:valueFrom="M300,70 l 0,-70 70,70 0,0 -70,70z" android:valueTo="M300,70 l 0,-70 70,0 0,140 -70,0 z" android:valueType="pathType"/> </set> </aapt:attr> </target> </animated-vector>
Modifier and Type | Class and Description |
---|---|
static class |
AnimatedVectorDrawable.VectorDrawableAnimatorRT |
Drawable.Callback, Drawable.ConstantState
Animatable2.AnimationCallback
Constructor and Description |
---|
AnimatedVectorDrawable() |
Modifier and Type | Method and Description |
---|---|
void |
applyTheme(Resources.Theme t)
Applies the specified theme to this Drawable and its children.
|
boolean |
canApplyTheme() |
boolean |
canReverse() |
void |
clearAnimationCallbacks()
Removes all existing animation callbacks.
|
void |
clearMutated()
Clears the mutated state, allowing this drawable to be cached and
mutated again.
|
void |
draw(Canvas canvas)
Draws the AnimatedVectorDrawable into the given canvas.
|
void |
forceAnimationOnUI()
Force to animate on UI thread.
|
int |
getAlpha()
For API 25 and later, AnimatedVectorDrawable runs on RenderThread.
|
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. |
int |
getIntrinsicHeight()
Returns the drawable's intrinsic height.
|
int |
getIntrinsicWidth()
Returns the drawable's intrinsic width.
|
int |
getOpacity()
Return the opacity/transparency of this Drawable.
|
Insets |
getOpticalInsets()
Return in insets the layout insets suggested by this Drawable for use with alignment
operations during layout.
|
void |
getOutline(Outline outline)
Called to get the drawable to populate the Outline that defines its drawing area.
|
void |
inflate(Resources res,
XmlPullParser parser,
AttributeSet attrs,
Resources.Theme theme)
Inflate this Drawable from an XML resource optionally styled by a theme.
|
boolean |
isRunning()
Indicates whether the animation is running.
|
boolean |
isStateful()
Indicates whether this drawable will change its appearance based on
state.
|
Drawable |
mutate()
Make this drawable mutable.
|
protected void |
onBoundsChange(Rect bounds)
Override this in your subclass to change appearance if you vary based on
the bounds.
|
boolean |
onLayoutDirectionChanged(int layoutDirection)
Called when the drawable's resolved layout direction changes.
|
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 |
registerAnimationCallback(Animatable2.AnimationCallback callback)
Adds a callback to listen to the animation events.
|
void |
reset()
Resets the AnimatedVectorDrawable to the start state as specified in the animators.
|
void |
reverse()
Reverses ongoing animations or starts pending animations in reverse.
|
void |
setAlpha(int alpha)
Specify an alpha value for the drawable. 0 means fully transparent, and
255 means fully opaque.
|
void |
setColorFilter(ColorFilter colorFilter)
Specify an optional color filter for the drawable.
|
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 |
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.
|
boolean |
unregisterAnimationCallback(Animatable2.AnimationCallback callback)
Removes the specified animation callback.
|
clearColorFilter, copyBounds, copyBounds, createFromPath, createFromResourceStream, createFromResourceStream, createFromStream, createFromXml, createFromXml, createFromXmlInner, createFromXmlInner, getBounds, getCallback, getCurrent, getDirtyBounds, getHotspotBounds, getLayoutDirection, getLevel, getMinimumHeight, getMinimumWidth, getPadding, getState, getTransparentRegion, inflate, invalidateSelf, isAutoMirrored, isFilterBitmap, isProjected, isVisible, jumpToCurrentState, obtainAttributes, parseTintMode, resolveOpacity, scheduleSelf, setAutoMirrored, setBounds, setBounds, setCallback, setChangingConfigurations, setColorFilter, setDither, setFilterBitmap, setLayoutDirection, setLevel, setState, setTint, setXfermode, unscheduleSelf
public Drawable mutate()
Drawable
mutate
in class Drawable
Drawable.ConstantState
,
Drawable.getConstantState()
public void clearMutated()
Drawable
This is hidden because only framework drawables can be cached, so custom drawables don't need to support constant state, mutate(), or clearMutated().
clearMutated
in class Drawable
public Drawable.ConstantState getConstantState()
Drawable
Drawable.ConstantState
instance that holds the shared state of this Drawable.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)
Note: Calling this method with a software canvas when the AnimatedVectorDrawable is being animated on RenderThread (for API 25 and later) may yield outdated result, as the UI thread is not guaranteed to be in sync with RenderThread on VectorDrawable's property changes during RenderThread animations.
protected void onBoundsChange(Rect bounds)
Drawable
onBoundsChange
in class Drawable
protected boolean onStateChange(int[] state)
Drawable
onStateChange
in class Drawable
protected boolean onLevelChange(int level)
Drawable
onLevelChange
in class Drawable
public boolean onLayoutDirectionChanged(int layoutDirection)
Drawable
onLayoutDirectionChanged
in class Drawable
layoutDirection
- the new resolved layout directiontrue
if the layout direction change has caused the
appearance of the drawable to change such that it needs to be
re-drawn, false
otherwiseDrawable.setLayoutDirection(int)
public int getAlpha()
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 ColorFilter getColorFilter()
Drawable
null
if none set.getColorFilter
in class Drawable
null
if none setpublic 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 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 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 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 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 void getOutline(Outline outline)
Drawable
This method is called by the default 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 Drawable
View.setOutlineProvider(android.view.ViewOutlineProvider)
public Insets getOpticalInsets()
Drawable
getOpticalInsets
in class Drawable
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 forceAnimationOnUI()
public boolean canApplyTheme()
canApplyTheme
in class Drawable
public void applyTheme(Resources.Theme t)
Drawable
applyTheme
in class Drawable
t
- the theme to applypublic boolean isRunning()
Animatable
isRunning
in interface Animatable
public void reset()
public void start()
Animatable
start
in interface Animatable
public void stop()
Animatable
stop
in interface Animatable
public void reverse()
NOTE: Only works if all animations support reverse. Otherwise, this will do nothing.
public boolean canReverse()
public void registerAnimationCallback(Animatable2.AnimationCallback callback)
Animatable2
registerAnimationCallback
in interface Animatable2
callback
- Callback to add.public boolean unregisterAnimationCallback(Animatable2.AnimationCallback callback)
Animatable2
unregisterAnimationCallback
in interface Animatable2
callback
- Callback to remove.false
if callback didn't exist in the call back list, or true
if
callback has been removed successfully.public void clearAnimationCallbacks()
Animatable2
clearAnimationCallbacks
in interface Animatable2