Blitheness resources

An animation resources can define i of two types of animations:

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

There are ii types of animations that you tin do with the view blitheness framework:

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

Property blitheness

An animation divers in XML that modifies backdrop of the target object, such as background color or blastoff value, over a gear up corporeality of time.

file location:
res/animator/filename.xml
The filename will be used as the resource ID.
compiled resource datatype:
Resource arrow to a ValueAnimator, ObjectAnimator, or AnimatorSet.
resource reference:
In Coffee-based or Kotlin code: R.animator.filename
In XML: @[packet:]animator/filename
syntax:
<fix   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" | "opposite"]         android:valueType=["intType" | "floatType"]/>      <animator         android:duration="int"         android:valueFrom="bladder                        |                        int                        |                        color"         android:valueTo="float                        |                        int                        |                        colour"         android:startOffset="int"         android:repeatCount="int"         android:repeatMode=["restart" | "reverse"]         android:valueType=["intType" | "floatType"]/>      <set>         ...     </gear up> </set>                      

The file must have a single root element: either <set>, <objectAnimator>, or <valueAnimator>. You can grouping blitheness elements together inside the <ready> element, including other <gear up> elements.

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

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

attributes:

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

attributes:

android:propertyName
String. Required. The object's property to animate, referenced by its name. For example yous can specify "alpha" or "backgroundColor" for a View object. The objectAnimator element does non expose a target attribute, however, and then you 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 gear up the target object that contains this belongings.
android:valueTo
float, int, or colour. Required. The value where the animated property ends. Colors are represented every bit 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 holding's get method. Colors are represented as six digit hexadecimal numbers (for example, #333333).
android:elapsing
int. The time in milliseconds of the blitheness. 300 milliseconds is the default.
android:startOffset
int. The amount of milliseconds the animation delays after starting time() is called.
android:repeatCount
int. How many times to repeat an animation. Set to "-ane" to infinitely echo or to a positive integer. For example, a value of "1" means that the animation is repeated once after the initial run of the blitheness, and then 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 information technology reaches the terminate of the blitheness. android:repeatCount must be set to a positive integer or "-i" for this aspect to have an effect. Set to "reverse" to have the blitheness opposite 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 blithe values are integers
floatType (default) Specifies that the animated values are floats
<animator>
Performs an animation over a specified amount of fourth dimension. Represents a ValueAnimator.

attributes:

android:valueTo
bladder, int, or colour. 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 instance, #333333).
android:duration
int. The fourth dimension in milliseconds of the blitheness. 300ms is the default.
android:startOffset
int. The amount of milliseconds the animation delays later start() is chosen.
android:repeatCount
int. How many times to echo an blitheness. Ready to "-i" to infinitely repeat or to a positive integer. For case, a value of "i" means that the blitheness is repeated once later on the initial run of the animation, 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 information technology reaches the cease of the animation. android:repeatCount must be set to a positive integer or "-1" for this aspect to accept an effect. Set to "reverse" to have the animation reverse direction with each iteration or "restart" to have the animation loop from the outset each time.
android:valueType
Keyword. Practice not specify this attribute if the value is a colour. 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">     <set>         <objectAnimator             android:propertyName="10"             android:elapsing="500"             android:valueTo="400"             android:valueType="intType"/>         <objectAnimator             android:propertyName="y"             android:elapsing="500"             android:valueTo="300"             android:valueType="intType"/>     </set up>     <objectAnimator         android:propertyName="blastoff"         android:elapsing="500"         android:valueTo="1f"/> </set>                        

In guild to run this animation, y'all must inflate the XML resources in your lawmaking to an AnimatorSet object, then set the target objects for all of the animations before starting the blitheness 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 set: AnimatorSet = AnimatorInflater.loadAnimator(myContext, R.animator.property_animator)     .employ {         setTarget(myObject)         starting time()     }                            

Coffee

AnimatorSet prepare = (AnimatorSet) AnimatorInflater.loadAnimator(myContext,     R.animator.property_animator); fix.setTarget(myObject); set.showtime();                            
see likewise:
  • Holding Animation
  • API Demos for examples on how to employ the property blitheness system.

View animation

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

Tween animation

An blitheness defined in XML that performs transitions such every bit rotating, fading, moving, and stretching on a graphic.

file location:
res/anim/filename.xml
The filename will be used every bit the resource ID.
compiled resource datatype:
Resource arrow to an Animation.
resource reference:
In Java: R.anim.filename
In XML: @[packet:]anim/filename
syntax:
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"     android:interpolator="@[packet:]anim/interpolator_resource"     android:shareInterpolator=["true" | "false"] >     <blastoff         android:fromAlpha="float"         android:toAlpha="float" />     <scale         android:fromXScale="float"         android:toXScale="float"         android:fromYScale="float"         android:toYScale="float"         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="float" />     <set>         ...     </set> </set up>                      

The file must take a unmarried root element: either an <alpha>, <calibration>, <translate>, <rotate>, or <set> element that holds a group (or groups) of other blitheness elements (even nested <gear up> elements).

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

attributes:

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

attributes:

android:fromAlpha
Bladder. Starting opacity offset, where 0.0 is transparent and ane.0 is opaque.
android:toAlpha
Bladder. 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 chemical element).

<scale>
A resizing animation. You can specify the eye point of the paradigm from which it grows outward (or inward) by specifying pivotX and pivotY. For instance, if these values are 0, 0 (elevation-left corner), all growth volition be downwards and to the correct. Represents a ScaleAnimation.

attributes:

android:fromXScale
Float. Starting X size get-go, where 1.0 is no change.
android:toXScale
Bladder. Ending X size offset, where i.0 is no change.
android:fromYScale
Float. Starting Y size offset, where i.0 is no change.
android:toYScale
Float. Catastrophe Y size first, where one.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 grade reference (of which, all XML attributes are inherited by this element).

<interpret>
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 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
Float or percentage. Starting X outset. Expressed either: in pixels relative to the normal position (such every bit "5"), in percentage relative to the element width (such as "5%"), or in percentage relative to the parent width (such every bit "5%p").
android:toXDelta
Bladder or percent. Ending X offset. Expressed either: in pixels relative to the normal position (such every bit "5"), in percentage relative to the element width (such as "5%"), or in percentage relative to the parent width (such equally "five%p").
android:fromYDelta
Float or percent. Starting Y starting time. Expressed either: in pixels relative to the normal position (such as "5"), in percentage relative to the chemical element acme (such every bit "5%"), or in percentage relative to the parent elevation (such as "5%p").
android:toYDelta
Float or percentage. Catastrophe Y commencement. Expressed either: in pixels relative to the normal position (such as "5"), in pct relative to the chemical element height (such as "5%"), or in percentage relative to the parent superlative (such as "5%p").

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

<rotate>
A rotation animation. Represents a RotateAnimation.

attributes:

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

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

instance:
XML file saved at res/anim/hyperspace_jump.xml:
<ready 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.6"         android:pivotX="50%"         android:pivotY="fifty%"         android:fillAfter="false"         android:duration="700" />     <prepare         android:interpolator="@android:anim/accelerate_interpolator"         android:startOffset="700">         <scale             android:fromXScale="one.4"             android:toXScale="0.0"             android:fromYScale="0.6"             android:toYScale="0.0"             android:pivotX="fifty%"             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> </set>                        

This awarding code volition apply the animation to an ImageView and start the blitheness:

Kotlin

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

Coffee

ImageView image = (ImageView) findViewById(R.id.image); Animation hyperspaceJump = AnimationUtils.loadAnimation(this, R.anim.hyperspace_jump); epitome.startAnimation(hyperspaceJump);                            
run across also:
  • 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 furnishings to be accelerated, decelerated, repeated, bounced, etc.

An interpolator is applied to an animation chemical 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 class. For each interpolator class, Android includes a public resource you lot can reference in club to use the interpolator to an animation using the android:interpolator attribute. The following tabular array specifies the resource to use for each interpolator:

Interpolator form Resources 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's how you can apply one of these with the android:interpolator attribute:

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

Custom interpolators

If you're not satisfied with the interpolators provided by the platform (listed in the table above), you tin can create a custom interpolator resources with modified attributes. For instance, you tin adjust the charge per unit of acceleration for the AnticipateInterpolator, or accommodate the number of cycles for the CycleInterpolator. In order to do so, y'all 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 resources datatype:
Resources pointer 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 yous don't utilise any attributes, then your interpolator will part exactly the same equally those provided by the platform (listed in the table higher up).

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

No attributes.

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

attributes:

android:factor
Float. The acceleration rate (default is 1).
<anticipateInterpolator>
The change starts astern then flings forrard.

attributes:

android:tension
Float. The corporeality of tension to apply (default is ii).
<anticipateOvershootInterpolator>
The change starts backward, flings forward and overshoots the target value, then settles at the last value.

attributes:

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

No attributes

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

attributes:

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

attributes:

android:factor
Bladder. The deceleration rate (default is 1).
<linearInterpolator>
The rate of modify is constant.

No attributes.

<overshootInterpolator>
The modify flings forrard and overshoots the last value, then comes back.

attributes:

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

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

<?xml version="one.0" encoding="utf-eight"?> <overshootInterpolator xmlns:android="http://schemas.android.com/apk/res/android"     android:tension="seven.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="1.0"     android:toXScale="3.0"     android:fromYScale="1.0"     android:toYScale="3.0"     android:pivotX="50%"     android:pivotY="50%"     android:duration="700" />                      

Frame blitheness

An blitheness defined in XML that shows a sequence of images in club (like a pic).

file location:
res/drawable/filename.xml
The filename will be used every bit the resource ID.
compiled resource datatype:
Resources pointer to an AnimationDrawable.
resource reference:
In Coffee: R.drawable.filename
In XML: @[parcel:]drawable.filename
syntax:
<?xml version="ane.0" encoding="utf-8"?> <animation-listing xmlns:android="http://schemas.android.com/apk/res/android"     android:oneshot=["true" | "imitation"] >     <particular         android:drawable="@[package:]drawable/drawable_resource_name"         android:elapsing="integer" /> </animation-list>                      
elements:
<animation-list>
Required. This must exist the root chemical element. Contains one or more <item> elements.

attributes:

android:oneshot
Boolean. "true" if you desire to perform the animation once; "imitation" to loop the animation.
<item>
A single frame of animation. Must be a child of a <animation-list> chemical element.

attributes:

android:drawable
Drawable resources. The drawable to use 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-eight"?> <blitheness-list xmlns:android="http://schemas.android.com/apk/res/android"     android:oneshot="false">     <item android:drawable="@drawable/rocket_thrust1" android:duration="200" />     <item android:drawable="@drawable/rocket_thrust2" android:duration="200" />     <item android:drawable="@drawable/rocket_thrust3" android:duration="200" /> </animation-list>                          
This application code will fix the animation every bit the background for a View, and so play the animation:
see also:
  • 2nd Graphics: Frame Animation