Blitheness resources

An animation resource can define one of two types of animations:

Property Animation
Creates an blitheness by modifying an object'south property values over a set menses of time with an Animator.
View Blitheness

There are two types of animations that you can practise with the view blitheness framework:

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

Property blitheness

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

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

The file must have a single root chemical element: either <set up>, <objectAnimator>, or <valueAnimator>. You tin can group animation elements together inside the <set up> element, including other <prepare> elements.

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

You tin specify nested <set up> tags to farther group animations together. Each <prepare> can define its own ordering attribute.

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 at the same time.
<objectAnimator>
Animates a specific property of an object over a specific amount of time. Represents an ObjectAnimator.

attributes:

android:propertyName
String. Required. The object'due south holding to animate, referenced by its name. For case you lot can specify "alpha" or "backgroundColor" for a View object. The objectAnimator element does not expose a target aspect, nonetheless, then you cannot set the object to animate in the XML declaration. You have to inflate your animation XML resources by calling loadAnimator() and call setTarget() to set the target object that contains this property.
android:valueTo
float, int, or color. Required. The value where the animated belongings ends. Colors are represented as half dozen digit hexadecimal numbers (for example, #333333).
android:valueFrom
float, int, or color. The value where the animated belongings starts. If not specified, the animation starts at the value obtained by the property'southward get method. Colors are represented every bit six digit hexadecimal numbers (for example, #333333).
android:duration
int. The fourth dimension in milliseconds of the blitheness. 300 milliseconds is the default.
android:startOffset
int. The corporeality of milliseconds the animation delays after start() is called.
android:repeatCount
int. How many times to repeat an animation. Ready to "-ane" to infinitely repeat or to a positive integer. For example, a value of "ane" ways that the animation is repeated once after the initial run of the animation, and so the animation plays a total of two times. The default value is "0", which means no repetition.
android:repeatMode
int. How an animation behaves when it reaches the finish of the animation. android:repeatCount must be set to a positive integer or "-1" for this attribute to accept an upshot. Set to "reverse" to have the animation reverse direction with each iteration or "restart" to have the animation loop from the commencement each time.
android:valueType
Keyword. Do non specify this attribute if the value is a color. The blitheness framework automatically handles colour values
Value Clarification
intType Specifies that the animated values are integers
floatType (default) Specifies that the animated values are floats
<animator>
Performs an animation over a specified amount of time. Represents a ValueAnimator.

attributes:

android:valueTo
float, int, or color. Required. The value where the animation ends. Colors are represented as six digit hexadecimal numbers (for instance, #333333).
android:valueFrom
bladder, int, or colour. 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 blitheness. 300ms is the default.
android:startOffset
int. The amount of milliseconds the blitheness delays subsequently outset() is called.
android:repeatCount
int. How many times to repeat an animation. Set to "-ane" to infinitely repeat or to a positive integer. For example, a value of "1" means that the animation is repeated once after the initial run of the animation, so the blitheness plays a total of two times. The default value is "0", which ways no repetition.
android:repeatMode
int. How an animation behaves when it reaches the end of the animation. android:repeatCount must be gear up to a positive integer or "-ane" for this attribute to have an effect. Set to "reverse" to have the animation reverse direction with each iteration or "restart" to take the animation loop from the beginning each fourth dimension.
android:valueType
Keyword. Practice not specify this attribute if the value is a color. The blitheness framework automatically handles color 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">     <gear up>         <objectAnimator             android:propertyName="ten"             android:elapsing="500"             android:valueTo="400"             android:valueType="intType"/>         <objectAnimator             android:propertyName="y"             android:duration="500"             android:valueTo="300"             android:valueType="intType"/>     </set>     <objectAnimator         android:propertyName="alpha"         android:elapsing="500"         android:valueTo="1f"/> </set up>                        

In order to run this animation, you must inflate the XML resources in your code to an AnimatorSet object, and so fix 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 equally a convenience. The following code shows how to do this:

Kotlin

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

Java

AnimatorSet fix = (AnimatorSet) AnimatorInflater.loadAnimator(myContext,     R.animator.property_animator); set.setTarget(myObject); set up.start();                            
see as well:
  • Property Animation
  • API Demos for examples on how to utilise the holding blitheness system.

View blitheness

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

Tween animation

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

file location:
res/anim/filename.xml
The filename will be used as the resource ID.
compiled resources datatype:
Resource arrow to an Blitheness.
resource reference:
In Java: R.anim.filename
In XML: @[package:]anim/filename
syntax:
<?xml version="ane.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"     android:interpolator="@[package:]anim/interpolator_resource"     android:shareInterpolator=["true" | "false"] >     <alpha         android:fromAlpha="bladder"         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="bladder" />     <rotate         android:fromDegrees="bladder"         android:toDegrees="float"         android:pivotX="bladder"         android:pivotY="float" />     <fix>         ...     </set> </gear up>                      

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

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

attributes:

android:interpolator
Interpolator resource. An Interpolator to apply on the animation. The value must be a reference to a resource that specifies an interpolator (non an interpolator course proper noun). At that place are default interpolator resource bachelor from the platform or y'all can create your own interpolator resources. See the discussion below for more than well-nigh Interpolators.
android:shareInterpolator
Boolean. "truthful" if y'all want to share the same interpolator among all child elements.
<blastoff>
A fade-in or fade-out animation. Represents an AlphaAnimation.

attributes:

android:fromAlpha
Bladder. Starting opacity first, where 0.0 is transparent and ane.0 is opaque.
android:toAlpha
Float. Ending opacity outset, where 0.0 is transparent and 1.0 is opaque.

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

<calibration>
A resizing blitheness. You tin specify the center point of the paradigm from which it grows outward (or inward) by specifying pivotX and pivotY. For example, if these values are 0, 0 (elevation-left corner), all growth volition be downward and to the right. Represents a ScaleAnimation.

attributes:

android:fromXScale
Float. Starting X size offset, where i.0 is no modify.
android:toXScale
Float. Ending Ten size offset, where 1.0 is no change.
android:fromYScale
Float. Starting Y size outset, where 1.0 is no change.
android:toYScale
Bladder. Ending Y size offset, where 1.0 is no change.
android:pivotX
Float. The 10 coordinate to remain fixed when the object is scaled.
android:pivotY
Bladder. The Y coordinate to remain fixed when the object is scaled.

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

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

attributes:

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

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

<rotate>
A rotation animation. Represents a RotateAnimation.

attributes:

android:fromDegrees
Float. Starting angular position, in degrees.
android:toDegrees
Float. Ending athwart position, in degrees.
android:pivotX
Float or percentage. The X coordinate of the eye of rotation. Expressed either: in pixels relative to the object'southward left border (such as "v"), in percentage relative to the object'south left edge (such as "5%"), or in percent relative to the parent container's left edge (such equally "five%p").
android:pivotY
Float or percentage. The Y coordinate of the heart of rotation. Expressed either: in pixels relative to the object'southward top edge (such as "five"), in percentage relative to the object's top border (such equally "5%"), or in percentage relative to the parent container's meridian edge (such as "5%p").

For more than attributes supported by <rotate>, meet the Animation course reference (of which, all XML attributes are inherited past 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">     <scale         android:interpolator="@android:anim/accelerate_decelerate_interpolator"         android:fromXScale="1.0"         android:toXScale="1.4"         android:fromYScale="1.0"         android:toYScale="0.six"         android:pivotX="50%"         android:pivotY="50%"         android:fillAfter="fake"         android:duration="700" />     <set         android:interpolator="@android:anim/accelerate_interpolator"         android:startOffset="700">         <calibration             android:fromXScale="1.4"             android:toXScale="0.0"             android:fromYScale="0.6"             android:toYScale="0.0"             android:pivotX="50%"             android:pivotY="50%"             android:elapsing="400" />         <rotate             android:fromDegrees="0"             android:toDegrees="-45"             android:toYScale="0.0"             android:pivotX="50%"             android:pivotY="50%"             android:duration="400" />     </set> </prepare>                        

This application code will apply the animation to an ImageView and offset the animation:

Kotlin

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

Java

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

Interpolators

An interpolator is an animation modifier defined in XML that affects the charge per unit of change in an animation. This allows your existing animation furnishings to be accelerated, decelerated, repeated, bounced, etc.

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

All interpolators bachelor in Android are subclasses of the Interpolator course. For each interpolator course, Android includes a public resource yous tin can reference in society to apply the interpolator to an animation using the android:interpolator aspect. The following table specifies the resource to use for each interpolator:

Interpolator class 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 you can apply ane of these with the android:interpolator attribute:

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

Custom interpolators

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

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

If you don't apply any attributes, then your interpolator will function exactly the aforementioned every bit those provided past the platform (listed in the table in a higher place).

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

No attributes.

<accelerateInterpolator>
The charge per unit of modify starts out slowly, then accelerates.

attributes:

android:factor
Float. The dispatch rate (default is one).
<anticipateInterpolator>
The change starts astern and then flings frontwards.

attributes:

android:tension
Float. The amount of tension to use (default is 2).
<anticipateOvershootInterpolator>
The modify starts backward, flings forward and overshoots the target value, so settles at the terminal value.

attributes:

android:tension
Float. The corporeality of tension to apply (default is 2).
android:extraTension
Float. The corporeality by which to multiply the tension (default is 1.5).
<bounceInterpolator>
The change bounces at the end.

No attributes

<cycleInterpolator>
Repeats the blitheness for a specified number of cycles. The rate of change follows a sinusoidal design.

attributes:

android:cycles
Integer. The number of cycles (default is one).
<decelerateInterpolator>
The charge per unit of change starts out quickly, then decelerates.

attributes:

android:factor
Float. The deceleration rate (default is i).
<linearInterpolator>
The rate of change is constant.

No attributes.

<overshootInterpolator>
The change flings forrad and overshoots the final value, then comes dorsum.

attributes:

android:tension
Float. The corporeality of tension to apply (default is 2).
instance:

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

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

This blitheness XML will employ the interpolator:

<scale 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="1.0"     android:toYScale="3.0"     android:pivotX="50%"     android:pivotY="50%"     android:elapsing="700" />                      

Frame blitheness

An animation defined in XML that shows a sequence of images in society (similar a film).

file location:
res/drawable/filename.xml
The filename will be used as the resource ID.
compiled resources datatype:
Resources pointer to an AnimationDrawable.
resource reference:
In Java: R.drawable.filename
In XML: @[packet:]drawable.filename
syntax:
<?xml version="ane.0" encoding="utf-viii"?> <blitheness-list xmlns:android="http://schemas.android.com/apk/res/android"     android:oneshot=["truthful" | "false"] >     <item         android:drawable="@[package:]drawable/drawable_resource_name"         android:duration="integer" /> </animation-list>                      
elements:
<animation-list>
Required. This must be the root element. Contains one or more than <item> elements.

attributes:

android:oneshot
Boolean. "true" if y'all want to perform the animation one time; "false" to loop the blitheness.
<particular>
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 duration to show this frame, in milliseconds.
instance:
XML file saved at res/drawable/rocket_thrust.xml:
<?xml version="i.0" encoding="utf-viii"?> <blitheness-listing xmlns:android="http://schemas.android.com/apk/res/android"     android:oneshot="false">     <particular android:drawable="@drawable/rocket_thrust1" android:duration="200" />     <particular android:drawable="@drawable/rocket_thrust2" android:elapsing="200" />     <item android:drawable="@drawable/rocket_thrust3" android:duration="200" /> </animation-list>                          
This application code will set the animation equally the background for a View, then play the blitheness:
meet also:
  • 2D Graphics: Frame Animation