Animation resources

An animation resources tin can ascertain one of two types of animations:

Property Animation
Creates an blitheness by modifying an object'due south holding values over a gear up menstruation of time with an Animator.
View Blitheness

There are 2 types of animations that you lot can do with the view blitheness framework:

  • Tween animation: Creates an blitheness by performing a series of transformations on a single image with an Animation
  • Frame blitheness: or creates an blitheness by showing a sequence of images in order with an AnimationDrawable.

Property animation

An animation divers in XML that modifies properties of the target object, such every bit background color or blastoff value, over a set amount of time.

file location:
res/animator/filename.xml
The filename volition be used as the resource ID.
compiled resource datatype:
Resource pointer to a ValueAnimator, ObjectAnimator, or AnimatorSet.
resource reference:
In Java-based or Kotlin code: R.animator.filename
In XML: @[package:]animator/filename
syntax:
<set   android:ordering=["together" | "sequentially"]>      <objectAnimator         android:propertyName="string"         android:duration="int"         android:valueFrom="float                        |                        int                        |                        color"         android:valueTo="bladder                        |                        int                        |                        color"         android:startOffset="int"         android:repeatCount="int"         android:repeatMode=["restart" | "opposite"]         android:valueType=["intType" | "floatType"]/>      <animator         android:duration="int"         android:valueFrom="float                        |                        int                        |                        color"         android:valueTo="bladder                        |                        int                        |                        colour"         android:startOffset="int"         android:repeatCount="int"         android:repeatMode=["restart" | "reverse"]         android:valueType=["intType" | "floatType"]/>      <set up>         ...     </set> </set>                      

The file must have a single root chemical element: either <gear up>, <objectAnimator>, or <valueAnimator>. Y'all can group animation elements together inside the <ready> chemical element, including other <set up> elements.

elements:
<set>
A container that holds other animation elements (<objectAnimator>, <valueAnimator>, or other <set> elements). Represents an AnimatorSet.

You can specify nested <set> tags to further group animations together. Each <set> can define its ain ordering aspect.

attributes:

android:ordering
Keyword. Specifies the play ordering of animations in this set.
Value Description
sequentially Play animations in this set sequentially
together (default) Play animations in this set up at the same time.
<objectAnimator>
Animates a specific property of an object over a specific corporeality of time. Represents an ObjectAnimator.

attributes:

android:propertyName
Cord. Required. The object's property to animate, referenced by its name. For instance you lot tin can specify "alpha" or "backgroundColor" for a View object. The objectAnimator element does not betrayal a target attribute, even so, then you cannot set the object to animate in the XML declaration. Y'all take to inflate your blitheness XML resources by calling loadAnimator() and telephone call setTarget() to set the target object that contains this belongings.
android:valueTo
float, int, or color. Required. The value where the blithe belongings ends. Colors are represented as 6 digit hexadecimal numbers (for example, #333333).
android:valueFrom
bladder, int, or color. The value where the animated property starts. If not specified, the animation starts at the value obtained by the property'southward get method. Colors are represented as six digit hexadecimal numbers (for example, #333333).
android:duration
int. The time in milliseconds of the animation. 300 milliseconds is the default.
android:startOffset
int. The amount of milliseconds the animation delays afterwards start() is called.
android:repeatCount
int. How many times to echo an blitheness. Set to "-1" to infinitely echo or to a positive integer. For instance, a value of "1" means that the animation is repeated one time after the initial run of the animation, so the animation plays a total of two times. The default value is "0", which ways no repetition.
android:repeatMode
int. How an blitheness behaves when it reaches the finish of the animation. android:repeatCount must be set to a positive integer or "-1" for this attribute to take an effect. Set to "reverse" to accept the animation contrary direction with each iteration or "restart" to have the animation loop from the beginning each time.
android:valueType
Keyword. Do not specify this attribute if the value is a color. The animation framework automatically handles color values
Value Description
intType Specifies that the animated values are integers
floatType (default) Specifies that the animated values are floats
<animator>
Performs an animation over a specified corporeality of time. Represents a ValueAnimator.

attributes:

android:valueTo
float, int, or color. Required. The value where the blitheness ends. Colors are represented every bit six digit hexadecimal numbers (for example, #333333).
android:valueFrom
float, int, or color. Required. The value where the animation starts. Colors are represented as six digit hexadecimal numbers (for instance, #333333).
android:duration
int. The time in milliseconds of the animation. 300ms is the default.
android:startOffset
int. The corporeality of milliseconds the animation delays later start() is called.
android:repeatCount
int. How many times to repeat an animation. Set to "-1" to infinitely repeat or to a positive integer. For example, a value of "ane" means that the animation is repeated once after the initial run of the blitheness, so the animation plays a total of two times. The default value is "0", which ways no repetition.
android:repeatMode
int. How an animation behaves when information technology reaches the stop of the blitheness. android:repeatCount must exist ready to a positive integer or "-1" for this attribute to have an consequence. Set up to "opposite" to have the animation reverse management with each iteration or "restart" to have the animation loop from the beginning each fourth dimension.
android:valueType
Keyword. Practise non specify this attribute if the value is a color. The animation framework automatically handles colour values.
Value Description
intType Specifies that the animated values are integers
floatType (default) Specifies that the animated values are floats
example:
XML file saved at res/animator/property_animator.xml:
<set android:ordering="sequentially">     <set up>         <objectAnimator             android:propertyName="x"             android:duration="500"             android:valueTo="400"             android:valueType="intType"/>         <objectAnimator             android:propertyName="y"             android:duration="500"             android:valueTo="300"             android:valueType="intType"/>     </prepare>     <objectAnimator         android:propertyName="alpha"         android:duration="500"         android:valueTo="1f"/> </ready>                        

In club to run this animation, you must inflate the XML resource in your code to an AnimatorSet object, and then set the target objects for all of the animations earlier starting the animation set. Calling setTarget() sets a single target object for all children of the AnimatorSet every bit a convenience. The following code shows how to exercise this:

Kotlin

val set: AnimatorSet = AnimatorInflater.loadAnimator(myContext, R.animator.property_animator)     .apply {         setTarget(myObject)         start()     }                            

Java

AnimatorSet ready = (AnimatorSet) AnimatorInflater.loadAnimator(myContext,     R.animator.property_animator); set.setTarget(myObject); set.start();                            
see also:
  • Property Animation
  • API Demos for examples on how to use the property animation system.

View blitheness

The view blitheness framework supports both tween and frame by frame animations, which can both exist declared in XML. The following sections depict how to use both methods.

Tween animation

An animation divers in XML that performs transitions such equally rotating, fading, moving, and stretching on a graphic.

file location:
res/anim/filename.xml
The filename will be used equally the resource ID.
compiled resource datatype:
Resource arrow to an Animation.
resource reference:
In Coffee: R.anim.filename
In XML: @[package:]anim/filename
syntax:
<?xml version="1.0" encoding="utf-8"?> <prepare xmlns:android="http://schemas.android.com/apk/res/android"     android:interpolator="@[packet:]anim/interpolator_resource"     android:shareInterpolator=["true" | "false"] >     <alpha         android:fromAlpha="float"         android:toAlpha="float" />     <scale         android:fromXScale="bladder"         android:toXScale="float"         android:fromYScale="float"         android:toYScale="bladder"         android:pivotX="float"         android:pivotY="float" />     <translate         android:fromXDelta="float"         android:toXDelta="float"         android:fromYDelta="bladder"         android:toYDelta="float" />     <rotate         android:fromDegrees="bladder"         android:toDegrees="float"         android:pivotX="float"         android:pivotY="bladder" />     <set up>         ...     </gear up> </fix>                      

The file must take a single root element: either an <blastoff>, <scale>, <translate>, <rotate>, or <set> chemical element that holds a group (or groups) of other animation elements (even nested <set> elements).

elements:
<prepare>
A container that holds other animation elements (<alpha>, <scale>, <interpret>, <rotate>) or other <set> elements. Represents an AnimationSet.

attributes:

android:interpolator
Interpolator resources. An Interpolator to use on the animation. The value must be a reference to a resource that specifies an interpolator (not an interpolator class name). There are default interpolator resources available from the platform or you can create your own interpolator resources. See the word below for more about Interpolators.
android:shareInterpolator
Boolean. "true" if you want to share the same interpolator among all child elements.
<alpha>
A fade-in or fade-out blitheness. Represents an AlphaAnimation.

attributes:

android:fromAlpha
Float. Starting opacity offset, where 0.0 is transparent and i.0 is opaque.
android:toAlpha
Float. Ending opacity offset, where 0.0 is transparent and 1.0 is opaque.

For more attributes supported past <alpha>, see the Animation class reference (of which, all XML attributes are inherited by this element).

<scale>
A resizing animation. You tin can specify the center point of the prototype from which it grows outward (or inward) by specifying pivotX and pivotY. For example, if these values are 0, 0 (superlative-left corner), all growth will be down and to the right. Represents a ScaleAnimation.

attributes:

android:fromXScale
Float. Starting X size starting time, where 1.0 is no change.
android:toXScale
Float. Ending X size start, where one.0 is no change.
android:fromYScale
Float. Starting Y size starting time, where ane.0 is no change.
android:toYScale
Bladder. Catastrophe Y size offset, where 1.0 is no modify.
android:pivotX
Float. The X coordinate to remain fixed when the object is scaled.
android:pivotY
Float. The Y coordinate to remain fixed when the object is scaled.

For more attributes supported by <scale>, run across the Blitheness class reference (of which, all XML attributes are inherited past this element).

<translate>
A vertical and/or horizontal motion. Supports the post-obit attributes in any of the following three formats: values from -100 to 100 ending with "%", indicating a percentage relative to itself; values from -100 to 100 catastrophe in "%p", indicating a percentage relative to its parent; a float value with no suffix, indicating an accented value. Represents a TranslateAnimation.

attributes:

android:fromXDelta
Float or per centum. Starting X kickoff. Expressed either: in pixels relative to the normal position (such every bit "five"), in percentage relative to the element width (such as "5%"), or in percent relative to the parent width (such as "5%p").
android:toXDelta
Bladder or percent. Ending 10 offset. Expressed either: in pixels relative to the normal position (such as "5"), in percentage relative to the element width (such as "five%"), or in percentage relative to the parent width (such as "five%p").
android:fromYDelta
Bladder or percentage. Starting Y offset. Expressed either: in pixels relative to the normal position (such every bit "v"), in percentage relative to the chemical element tiptop (such as "5%"), or in percentage relative to the parent pinnacle (such every bit "5%p").
android:toYDelta
Bladder or percentage. Ending Y start. Expressed either: in pixels relative to the normal position (such as "5"), in percentage relative to the chemical element height (such as "5%"), or in percent relative to the parent height (such every bit "five%p").

For more attributes supported past <interpret>, see the Animation grade reference (of which, all XML attributes are inherited by this element).

<rotate>
A rotation animation. Represents a RotateAnimation.

attributes:

android:fromDegrees
Float. Starting angular position, in degrees.
android:toDegrees
Bladder. Ending angular position, in degrees.
android:pivotX
Float or percentage. The X coordinate of the center of rotation. Expressed either: in pixels relative to the object'southward left edge (such equally "5"), in percent relative to the object'south left edge (such as "5%"), or in percentage relative to the parent container's left edge (such as "5%p").
android:pivotY
Float or percentage. The Y coordinate of the center of rotation. Expressed either: in pixels relative to the object's height edge (such as "five"), in per centum relative to the object'due south tiptop edge (such as "5%"), or in percent relative to the parent container's top edge (such equally "5%p").

For more than attributes supported past <rotate>, see the Animation class reference (of which, all XML attributes are inherited by this chemical element).

example:
XML file saved at res/anim/hyperspace_jump.xml:
<set xmlns:android="http://schemas.android.com/apk/res/android"     android:shareInterpolator="false">     <calibration         android:interpolator="@android:anim/accelerate_decelerate_interpolator"         android:fromXScale="1.0"         android:toXScale="1.iv"         android:fromYScale="1.0"         android:toYScale="0.half dozen"         android:pivotX="l%"         android:pivotY="l%"         android:fillAfter="false"         android:duration="700" />     <set         android:interpolator="@android:anim/accelerate_interpolator"         android:startOffset="700">         <calibration             android:fromXScale="1.four"             android:toXScale="0.0"             android:fromYScale="0.6"             android:toYScale="0.0"             android:pivotX="50%"             android:pivotY="50%"             android:duration="400" />         <rotate             android:fromDegrees="0"             android:toDegrees="-45"             android:toYScale="0.0"             android:pivotX="fifty%"             android:pivotY="50%"             android:duration="400" />     </set> </prepare>                        

This application code will use the animation to an ImageView and commencement the animation:

Kotlin

val image: ImageView = findViewById(R.id.prototype) val hyperspaceJump: Blitheness = AnimationUtils.loadAnimation(this, R.anim.hyperspace_jump) image.startAnimation(hyperspaceJump)                            

Java

ImageView paradigm = (ImageView) findViewById(R.id.epitome); Blitheness hyperspaceJump = AnimationUtils.loadAnimation(this, R.anim.hyperspace_jump); image.startAnimation(hyperspaceJump);                            
see likewise:
  • 2D Graphics: Tween Animation

Interpolators

An interpolator is an animation modifier defined in XML that affects the rate of change in an blitheness. This allows your existing animation effects to be accelerated, decelerated, repeated, bounced, etc.

An interpolator is applied to an animation element with the android:interpolator attribute, the value of which is a reference to an interpolator resource.

All interpolators available in Android are subclasses of the Interpolator form. For each interpolator course, Android includes a public resources you tin can reference in lodge to apply the interpolator to an animation using the android:interpolator attribute. The following table specifies the resource to use for each interpolator:

Interpolator course Resource ID
AccelerateDecelerateInterpolator @android:anim/accelerate_decelerate_interpolator
AccelerateInterpolator @android:anim/accelerate_interpolator
AnticipateInterpolator @android:anim/anticipate_interpolator
AnticipateOvershootInterpolator @android:anim/anticipate_overshoot_interpolator
BounceInterpolator @android:anim/bounce_interpolator
CycleInterpolator @android:anim/cycle_interpolator
DecelerateInterpolator @android:anim/decelerate_interpolator
LinearInterpolator @android:anim/linear_interpolator
OvershootInterpolator @android:anim/overshoot_interpolator

Hither's how yous can apply one of these with the android:interpolator attribute:

<prepare android:interpolator="@android:anim/accelerate_interpolator">     ... </set>                  

Custom interpolators

If you're not satisfied with the interpolators provided by the platform (listed in the table above), yous can create a custom interpolator resources with modified attributes. For instance, you can adjust the charge per unit of acceleration for the AnticipateInterpolator, or accommodate the number of cycles for the CycleInterpolator. In order to do and so, you need to create your own interpolator resource in an XML file.

file location:
res/anim/filename.xml
The filename will be used as the resource ID.
compiled resource datatype:
Resource pointer to the corresponding interpolator object.
resource reference:
In XML: @[bundle:]anim/filename
syntax:
<?xml version="1.0" encoding="utf-viii"?> <InterpolatorName                        xmlns:android="http://schemas.android.com/apk/res/android"     android:attribute_name="value"     />                      

If you don't use whatsoever attributes, so your interpolator will function exactly the same as those provided by the platform (listed in the tabular array higher up).

elements:
Notice that each Interpolator implementation, when defined in XML, begins its proper noun in lowercase.
<accelerateDecelerateInterpolator>
The rate of change starts and ends slowly but accelerates through the middle.

No attributes.

<accelerateInterpolator>
The rate of change starts out slowly, then accelerates.

attributes:

android:factor
Float. The dispatch charge per unit (default is one).
<anticipateInterpolator>
The change starts backward then flings forward.

attributes:

android:tension
Float. The amount of tension to apply (default is 2).
<anticipateOvershootInterpolator>
The alter starts astern, flings frontward and overshoots the target value, then settles at the final value.

attributes:

android:tension
Float. The corporeality of tension to apply (default is ii).
android:extraTension
Bladder. The amount by which to multiply the tension (default is 1.five).
<bounceInterpolator>
The change bounces at the terminate.

No attributes

<cycleInterpolator>
Repeats the animation for a specified number of cycles. The rate of modify follows a sinusoidal pattern.

attributes:

android:cycles
Integer. The number of cycles (default is 1).
<decelerateInterpolator>
The rate of change starts out quickly, and so decelerates.

attributes:

android:factor
Float. The deceleration charge per unit (default is 1).
<linearInterpolator>
The rate of alter is abiding.

No attributes.

<overshootInterpolator>
The change flings forward and overshoots the last value, then comes back.

attributes:

android:tension
Float. The amount of tension to utilise (default is ii).
case:

XML file saved at res/anim/my_overshoot_interpolator.xml:

<?xml version="1.0" encoding="utf-8"?> <overshootInterpolator xmlns:android="http://schemas.android.com/apk/res/android"     android:tension="vii.0"     />                      

This blitheness XML will apply the interpolator:

<calibration xmlns:android="http://schemas.android.com/apk/res/android"     android:interpolator="@anim/my_overshoot_interpolator"     android:fromXScale="1.0"     android:toXScale="iii.0"     android:fromYScale="i.0"     android:toYScale="iii.0"     android:pivotX="50%"     android:pivotY="50%"     android:duration="700" />                      

Frame blitheness

An blitheness defined in XML that shows a sequence of images in order (like a film).

file location:
res/drawable/filename.xml
The filename will be used equally the resources ID.
compiled resource datatype:
Resource arrow to an AnimationDrawable.
resources reference:
In Java: R.drawable.filename
In XML: @[package:]drawable.filename
syntax:
<?xml version="1.0" encoding="utf-8"?> <animation-list xmlns:android="http://schemas.android.com/apk/res/android"     android:oneshot=["truthful" | "fake"] >     <item         android:drawable="@[package:]drawable/drawable_resource_name"         android:duration="integer" /> </animation-list>                      
elements:
<animation-list>
Required. This must be the root element. Contains 1 or more than <item> elements.

attributes:

android:oneshot
Boolean. "true" if you want to perform the animation once; "false" to loop the animation.
<item>
A unmarried frame of blitheness. Must be a child of a <animation-list> element.

attributes:

android:drawable
Drawable resources. The drawable to use for this frame.
android:duration
Integer. The elapsing to testify this frame, in milliseconds.
example:
XML file saved at res/drawable/rocket_thrust.xml:
<?xml version="1.0" encoding="utf-viii"?> <animation-listing xmlns:android="http://schemas.android.com/apk/res/android"     android:oneshot="false">     <item android:drawable="@drawable/rocket_thrust1" android:elapsing="200" />     <item android:drawable="@drawable/rocket_thrust2" android:duration="200" />     <item android:drawable="@drawable/rocket_thrust3" android:duration="200" /> </animation-listing>                          
This application code will set the blitheness as the background for a View, then play the blitheness:
see as well:
  • 2d Graphics: Frame Animation