Animation resource

An blitheness resource tin can define one of ii types of animations:

Property Blitheness
Creates an blitheness by modifying an object's property values over a fix period of time with an Animator.
View Animation

At that place are two types of animations that y'all can do with the view animation framework:

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

Property animation

An blitheness divers in XML that modifies properties of the target object, such as background color or alpha value, over a set amount of time.

file location:
res/animator/filename.xml
The filename will exist used equally 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: @[parcel:]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:elapsing="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>         ...     </prepare> </gear up>                      

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

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

You can specify nested <fix> tags to further grouping animations together. Each <set> can define its own ordering attribute.

attributes:

android:ordering
Keyword. Specifies the play ordering of animations in this fix.
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 property to animate, referenced by its name. For example you can specify "alpha" or "backgroundColor" for a View object. The objectAnimator element does not expose a target attribute, however, so yous cannot set the object to animate in the XML annunciation. You have to inflate your animation XML resource by calling loadAnimator() and phone call setTarget() to set up the target object that contains this property.
android:valueTo
float, int, or colour. Required. The value where the animated property ends. Colors are represented as six digit hexadecimal numbers (for example, #333333).
android:valueFrom
float, int, or color. The value where the animated property starts. If not specified, the animation starts at the value obtained by the property's go method. Colors are represented as six digit hexadecimal numbers (for example, #333333).
android:elapsing
int. The time in milliseconds of the animation. 300 milliseconds is the default.
android:startOffset
int. The corporeality of milliseconds the animation delays subsequently outset() is called.
android:repeatCount
int. How many times to echo an animation. Set to "-1" to infinitely repeat or to a positive integer. For example, a value of "i" means that the animation is repeated in one case subsequently the initial run of the animation, so the blitheness plays a full of two times. The default value is "0", which ways no repetition.
android:repeatMode
int. How an animation behaves when information technology reaches the finish of the animation. android:repeatCount must be set up to a positive integer or "-1" for this attribute to have an effect. Set 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. 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 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 case, #333333).
android:valueFrom
float, int, or colour. Required. The value where the animation starts. Colors are represented as six digit hexadecimal numbers (for example, #333333).
android:duration
int. The fourth dimension in milliseconds of the animation. 300ms 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 animation. Gear up to "-1" to infinitely echo or to a positive integer. For example, a value of "ane" means that the animation is repeated once afterward the initial run of the animation, so the blitheness 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 end of the animation. android:repeatCount must be set to a positive integer or "-i" for this attribute to accept an upshot. Set up to "reverse" to take the animation reverse direction with each iteration or "restart" to accept the animation loop from the commencement each fourth dimension.
android:valueType
Keyword. Do not 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
example:
XML file saved at res/animator/property_animator.xml:
<set up android:ordering="sequentially">     <set up>         <objectAnimator             android:propertyName="x"             android:elapsing="500"             android:valueTo="400"             android:valueType="intType"/>         <objectAnimator             android:propertyName="y"             android:duration="500"             android:valueTo="300"             android:valueType="intType"/>     </gear up>     <objectAnimator         android:propertyName="blastoff"         android:duration="500"         android:valueTo="1f"/> </set>                        

In order to run this animation, y'all must inflate the XML resources in your lawmaking to an AnimatorSet object, and and so set up the target objects for all of the animations before starting the animation set. Calling setTarget() sets a single target object for all children of the AnimatorSet as a convenience. The following lawmaking shows how to practice this:

Kotlin

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

Coffee

AnimatorSet prepare = (AnimatorSet) AnimatorInflater.loadAnimator(myContext,     R.animator.property_animator); set.setTarget(myObject); set up.start();                            
come across also:
  • Property Animation
  • API Demos for examples on how to utilise the property animation organisation.

View animation

The view animation framework supports both tween and frame by frame animations, which tin can both exist declared in XML. The post-obit sections draw how to employ 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 exist used as the resources ID.
compiled resource datatype:
Resource arrow to an Animation.
resource reference:
In Java: R.anim.filename
In XML: @[package:]anim/filename
syntax:
<?xml version="1.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" />     <calibration         android:fromXScale="bladder"         android:toXScale="float"         android:fromYScale="float"         android:toYScale="float"         android:pivotX="bladder"         android:pivotY="bladder" />     <translate         android:fromXDelta="bladder"         android:toXDelta="float"         android:fromYDelta="bladder"         android:toYDelta="bladder" />     <rotate         android:fromDegrees="bladder"         android:toDegrees="float"         android:pivotX="float"         android:pivotY="float" />     <set>         ...     </set> </set>                      

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

elements:
<set>
A container that holds other animation elements (<blastoff>, <calibration>, <translate>, <rotate>) or other <fix> elements. Represents an AnimationSet.

attributes:

android:interpolator
Interpolator resource. An Interpolator to apply on the blitheness. The value must be a reference to a resource that specifies an interpolator (not an interpolator grade name). At that place are default interpolator resources available from the platform or you lot can create your ain interpolator resources. See the discussion below for more near Interpolators.
android:shareInterpolator
Boolean. "true" if you want to share the same interpolator among all kid elements.
<alpha>
A fade-in or fade-out animation. Represents an AlphaAnimation.

attributes:

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

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

<calibration>
A resizing animation. Y'all can specify the center point of the image from which it grows outward (or inward) by specifying pivotX and pivotY. For case, if these values are 0, 0 (tiptop-left corner), all growth will exist downward and to the right. Represents a ScaleAnimation.

attributes:

android:fromXScale
Float. Starting Ten size beginning, where one.0 is no alter.
android:toXScale
Float. Ending 10 size offset, where 1.0 is no change.
android:fromYScale
Float. Starting Y size offset, where ane.0 is no modify.
android:toYScale
Float. Catastrophe Y size offset, where 1.0 is no change.
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>, meet the Animation class reference (of which, all XML attributes are inherited by this element).

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

attributes:

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

For more attributes supported by <translate>, run into the Animation class 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
Float. Ending athwart position, in degrees.
android:pivotX
Float or per centum. The Ten coordinate of the heart of rotation. Expressed either: in pixels relative to the object's left edge (such as "5"), in per centum relative to the object'southward left edge (such every bit "v%"), or in percentage relative to the parent container's left edge (such equally "5%p").
android:pivotY
Bladder or percentage. The Y coordinate of the heart of rotation. Expressed either: in pixels relative to the object's top edge (such equally "v"), in per centum relative to the object's top edge (such as "v%"), or in per centum relative to the parent container's top edge (such every bit "v%p").

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

instance:
XML file saved at res/anim/hyperspace_jump.xml:
<set xmlns:android="http://schemas.android.com/apk/res/android"     android:shareInterpolator="simulated">     <scale         android:interpolator="@android:anim/accelerate_decelerate_interpolator"         android:fromXScale="1.0"         android:toXScale="1.4"         android:fromYScale="1.0"         android:toYScale="0.6"         android:pivotX="l%"         android:pivotY="50%"         android:fillAfter="false"         android:duration="700" />     <set         android:interpolator="@android:anim/accelerate_interpolator"         android:startOffset="700">         <scale             android:fromXScale="i.4"             android:toXScale="0.0"             android:fromYScale="0.six"             android:toYScale="0.0"             android:pivotX="fifty%"             android:pivotY="50%"             android:duration="400" />         <rotate             android:fromDegrees="0"             android:toDegrees="-45"             android:toYScale="0.0"             android:pivotX="50%"             android:pivotY="l%"             android:elapsing="400" />     </set> </gear up>                        

This application code will utilize the blitheness to an ImageView and get-go the animation:

Kotlin

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

Java

ImageView image = (ImageView) findViewById(R.id.paradigm); Animation hyperspaceJump = AnimationUtils.loadAnimation(this, R.anim.hyperspace_jump); image.startAnimation(hyperspaceJump);                            
encounter as well:
  • 2D Graphics: Tween Animation

Interpolators

An interpolator is an animation modifier defined in XML that affects the rate of change in an animation. This allows your existing blitheness effects 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 available in Android are subclasses of the Interpolator class. For each interpolator class, Android includes a public resource yous can reference in lodge to apply the interpolator to an blitheness using the android:interpolator aspect. The following tabular array 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

Here'south how you tin can use one of these with the android:interpolator attribute:

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

Custom interpolators

If you're not satisfied with the interpolators provided by the platform (listed in the tabular array above), you tin create a custom interpolator resources with modified attributes. For case, you tin can suit the rate of dispatch for the AnticipateInterpolator, or adjust the number of cycles for the CycleInterpolator. In social club 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 as the resource ID.
compiled resource datatype:
Resource pointer to the respective interpolator object.
resource reference:
In XML: @[bundle:]anim/filename
syntax:
<?xml version="1.0" encoding="utf-eight"?> <InterpolatorName                        xmlns:android="http://schemas.android.com/apk/res/android"     android:attribute_name="value"     />                      

If yous don't apply any attributes, and so your interpolator will role exactly the aforementioned equally those provided by the platform (listed in the table to a higher place).

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

No attributes.

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

attributes:

android:factor
Bladder. The dispatch rate (default is 1).
<anticipateInterpolator>
The change starts backward then flings forward.

attributes:

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

attributes:

android:tension
Bladder. The amount of tension to apply (default is ii).
android:extraTension
Float. The amount past which to multiply the tension (default is one.v).
<bounceInterpolator>
The change bounces at the terminate.

No attributes

<cycleInterpolator>
Repeats the animation for a specified number of cycles. The charge per unit of change follows a sinusoidal pattern.

attributes:

android:cycles
Integer. The number of cycles (default is 1).
<decelerateInterpolator>
The rate of modify starts out chop-chop, then decelerates.

attributes:

android:factor
Float. The deceleration rate (default is one).
<linearInterpolator>
The rate of alter is abiding.

No attributes.

<overshootInterpolator>
The change flings forwards and overshoots the terminal value, then comes back.

attributes:

android:tension
Bladder. The amount of tension to apply (default is 2).
example:

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="7.0"     />                      

This animation XML will apply the interpolator:

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

Frame animation

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

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

attributes:

android:oneshot
Boolean. "true" if yous want to perform the animation one time; "false" to loop the animation.
<item>
A single frame of animation. Must exist a kid of a <animation-list> element.

attributes:

android:drawable
Drawable resource. The drawable to employ for this frame.
android:duration
Integer. The duration 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="fake">     <item android:drawable="@drawable/rocket_thrust1" android:elapsing="200" />     <detail android:drawable="@drawable/rocket_thrust2" android:duration="200" />     <item android:drawable="@drawable/rocket_thrust3" android:duration="200" /> </animation-list>                          
This awarding code will fix the animation equally the background for a View, then play the animation:
encounter too:
  • 2d Graphics: Frame Animation