1page.title=Animation Resources
2parent.title=Resource Types
3parent.link=available-resources.html
4@jd:body
5
6<div id="qv-wrapper">
7  <div id="qv">
8   <h2>In this document</h2>
9    <ol>
10      <li><a href="#Property">Property Animation</a></li>
11      <li><a href="#View">View Animation</a>
12        <ol>
13          <li><a href="#Tween">Tween animation</a></li>
14          <li><a href="#Frame">Frame animation</a></li>
15        </ol>      
16      </li>
17    </ol>
18    <h2>See also</h2>
19    <ol>
20      <li><a href="{@docRoot}guide/topics/graphics/view-animation.html">View Animation</a></li>
21      <li><a href="{@docRoot}guide/topics/graphics/prop-animation.html">Property Animation</a></li>
22    </ol>
23  </div>
24</div>
25
26
27<p>An animation resource can define one of two types of animations:</p>
28
29<dl>
30  <dt><a href="#Property">Property Animation</a></dt>
31  <dd>Creates an animation by modifying an object's property values over a set period
32  of time with an {@link android.animation.Animator}.</dd>
33  <dt><a href="#View">View Animation</a></dt>
34  <dd>
35  <p>There are two types of animations that you can do with the view animation framework:</p>
36  <ul>
37    <li><a href="#Tween">Tween animation</a>: Creates an animation by performing a series of transformations on a single image
38    with an {@link android.view.animation.Animation}</li>
39    <li><a href="#Frame">Frame animation</a>: or creates an animation by showing a sequence of images in order
40    with an {@link android.graphics.drawable.AnimationDrawable}.</li>
41  </ul>
42  </dd>
43</dl>
44
45
46<h2 id="Property">Property Animation</h2>
47<p>An animation defined in XML that modifies properties of the target object, such as
48background color or alpha value, over a set amount of time.</p>
49
50<dl class="xml">
51
52<dt>file location:</dt>
53<dd><code>res/animator/<em>filename</em>.xml</code><br/>
54The filename will be used as the resource ID.</dd>
55
56<dt>compiled resource datatype:</dt>
57<dd>Resource pointer to a {@link android.animation.ValueAnimator}, {@link android.animation.ObjectAnimator},
58or {@link android.animation.AnimatorSet}.</dd>
59
60<dt>resource reference:</dt>
61<dd>
62In Java: <code>R.animator.<em>filename</em></code><br/>
63In XML: <code>@[<em>package</em>:]animator/<em>filename</em></code>
64</dd>
65
66<dt>syntax:</dt>
67<dd>
68<pre class="stx">
69&lt;<a href="#animator-set-element">set</a>
70  android:ordering=["together" | "sequentially"]&gt;
71
72    &lt;<a href="#obj-animator-element">objectAnimator</a>
73        android:propertyName="<em>string</em>"
74        android:duration="<em>int</em>"
75        android:valueFrom="<em>float</em> | <em>int</em> | <em>color</em>"
76        android:valueTo="<em>float</em> | <em>int</em> | <em>color</em>"
77        android:startOffset="<em>int</em>"
78        android:repeatCount="<em>int</em>"
79        android:repeatMode=["repeat" | "reverse"]
80        android:valueType=["intType" | "floatType"]/&gt;
81
82    &lt;<a href="#val-animator-element">animator</a>
83        android:duration="<em>int</em>"
84        android:valueFrom="<em>float</em> | <em>int</em> | <em>color</em>"
85        android:valueTo="<em>float</em> | <em>int</em> | <em>color</em>"
86        android:startOffset="<em>int</em>"
87        android:repeatCount="<em>int</em>"
88        android:repeatMode=["repeat" | "reverse"]
89        android:valueType=["intType" | "floatType"]/&gt;
90
91    &lt;<a href="#animator-set-element">set</a>&gt;
92        ...
93    &lt;/set&gt;
94&lt;/set&gt;
95</pre>
96
97<p>The file must have a single root element: either 
98<code>&lt;set&gt;</code>, <code>&lt;objectAnimator&gt;</code>, or <code>&lt;valueAnimator&gt;</code>. You can
99group animation elements together inside the <code>&lt;set&gt;</code> element, including other
100<code>&lt;set&gt;</code> elements.
101</p>
102</dd>
103
104<dt>elements:</dt>
105  <dd>
106  <dl class="tag-list">
107    <dt id="animator-set-element"><code>&lt;set&gt;</code></dt>
108    <dd>A container that holds other animation elements (<code>&lt;objectAnimator&gt;</code>,
109    <code>&lt;valueAnimator&gt;</code>, or other <code>&lt;set&gt;</code> elements).  Represents
110    an {@link android.animation.AnimatorSet}.
111    <p>You can specify nested <code>&lt;set&gt;</code> tags to further
112    group animations together. Each <code>&lt;set&gt;</code> can define its own 
113    <code>ordering</code> attribute.</p>
114
115      <p class="caps">attributes:</p>
116      <dl class="atn-list">
117        <dt>
118          <code>android:ordering</code>
119        </dt>
120        <dd>
121          <em>Keyword</em>. Specifies the play ordering of animations in this set.
122          <table> 
123            <tr><th>Value</th><th>Description</th></tr> 
124            <tr><td><code>sequentially</code></td><td>Play animations in this set sequentially</td></tr> 
125            <tr><td><code>together</code> (default)</td><td>Play animations in this set at the same time.</td></tr> 
126          </table> 
127        </dd>
128      </dl>
129    </dd>
130
131  <dt id="obj-animator-element"><code>&lt;objectAnimator&gt;</code></dt>
132    <dd>Animates a specific property of an object over a specific amount of time. Represents
133    an {@link android.animation.ObjectAnimator}.</p>
134    
135      <p class="caps">attributes:</p>
136      <dl class="atn-list">
137        <dt>
138          <code>android:propertyName</code> 
139        </dt>
140        <dd>
141          <em>String</em>. <strong>Required</strong>. The object's property to animate, referenced by its name. For example you can specify
142          <code>"alpha"</code> or <code>"backgroundColor"</code> for a View object.
143          The <code>objectAnimator</code> element does not expose a <code>target</code>
144          attribute, however, so you cannot set the object to animate in the XML declaration. You have to
145          inflate your animation XML resource by calling {@link android.animation.AnimatorInflater#loadAnimator
146          loadAnimator()} and call {@link android.animation.ObjectAnimator#setTarget setTarget()} to set
147          the target object that contains this property.
148        </dd>
149
150        <dt>
151          <code>android:valueTo</code>
152        </dt>
153        <dd>
154          <em>float, int, or color</em>. <strong>Required</strong>. The value where the animated property ends. Colors are represented
155          as six digit hexadecimal numbers (for example, #333333).
156        </dd>
157
158        <dt>
159          <code>android:valueFrom</code>
160        </dt>
161        <dd>
162          <em>float, int, or color</em>. The value where the animated property starts. If not
163          specified, the animation starts at the value obtained by the property's get method. Colors are represented
164          as six digit hexadecimal numbers (for example, #333333).
165        </dd>
166
167        <dt>
168          <code>android:duration</code>
169        </dt>
170        <dd>
171          <em>int</em>. The time in milliseconds of the animation. 300 milliseconds is the default.
172        </dd>
173
174        <dt>
175          <code>android:startOffset</code>
176        </dt>
177        <dd>
178          <em>int</em>. The amount of milliseconds the animation delays after
179          {@link android.animation.ObjectAnimator#start start()} is called.
180        </dd>
181
182        <dt>
183          <code>android:repeatCount</code>
184        </dt>
185        <dd>
186          <em>int</em>. How many times to repeat an animation. Set to <code>"-1"</code> to infinitely
187          repeat or to a positive integer. For example, a value of <code>"1"</code> means that the animation
188          is repeated once after the initial run of the animation, so the animation plays a total
189          of two times. The default value is <code>"0"</code>, which means no repetition.
190
191        </dd>
192
193        <dt>
194          <code>android:repeatMode</code>
195        </dt>
196        <dd>
197          <em>int</em>. How an animation behaves when it reaches the end of the animation. <code>android:repeatCount</code>
198          must be set to a positive integer or <code>"-1"</code> for this attribute to have an effect. Set to <code>"reverse"</code>
199          to have the animation reverse direction with each iteration or <code>"repeat"</code> to have the animation
200          loop from the beginning each time.
201        </dd>
202
203        <dt>
204          <code>android:valueType</code>
205        </dt>
206        <dd>
207          <em>Keyword</em>. Do not specify this attribute if the value is a color. The animation framework automatically handles color
208          values
209          <table> 
210            <tr><th>Value</th><th>Description</th></tr> 
211            <tr><td><code>intType</code></td><td>Specifies that the animated values are integers</td></tr> 
212            <tr><td><code>floatType</code> (default)</td><td>Specifies that the animated values are floats</td></tr> 
213          </table> 
214        </dd>
215
216      </dl>
217    </dd>
218
219<dt id="val-animator-element"><code>&lt;animator&gt;</code></dt>
220    <dd>Performs an animation over a specified amount of time.
221    Represents a {@link android.animation.ValueAnimator}.
222
223      <p class="caps">attributes:</p>
224      <dl class="atn-list">
225        <dt>
226          <code>android:valueTo</code>
227        </dt>
228        <dd>
229          <em>float, int, or color</em>. <strong>Required</strong>. The value where the animation ends. Colors are represented
230          as six digit hexadecimal numbers (for example, #333333).
231        </dd>
232
233        <dt>
234          <code>android:valueFrom</code>
235        </dt>
236        <dd>
237          <em>float, int, or color</em>. <strong>Required</strong>. The value where the animation starts. Colors are represented
238          as six digit hexadecimal numbers (for example, #333333).
239        </dd>
240
241        <dt>
242          <code>android:duration</code>
243        </dt>
244        <dd>
245          <em>int</em>. The time in milliseconds of the animation. 300ms is the default.
246        </dd>
247
248        <dt>
249          <code>android:startOffset</code>
250        </dt>
251        <dd>
252          <em>int</em>. The amount of milliseconds the animation delays after
253          {@link android.animation.ValueAnimator#start start()} is called.
254        </dd>
255
256        <dt>
257          <code>android:repeatCount</code>
258        </dt>
259        <dd>
260          <em>int</em>. How many times to repeat an animation. Set to <code>"-1"</code> to infinitely
261          repeat or to a positive integer. For example, a value of <code>"1"</code> means that the animation
262          is repeated once after the initial run of the animation, so the animation plays a total
263          of two times. The default value is <code>"0"</code>, which means no repetition.
264        </dd>
265
266        <dt>
267          <code>android:repeatMode</code>
268        </dt>
269        <dd>
270          <em>int</em>. How an animation behaves when it reaches the end of the animation. <code>android:repeatCount</code>
271          must be set to a positive integer or <code>"-1"</code> for this attribute to have an effect. Set to <code>"reverse"</code>
272          to have the animation reverse direction with each iteration or <code>"repeat"</code> to have the animation
273          loop from the beginning each time.
274        </dd>
275
276        <dt>
277          <code>android:valueType</code>
278        </dt>
279        <dd>
280          <em>Keyword</em>. Do not specify this attribute if the value is a color. The animation framework automatically handles color
281          values.
282          <table> 
283            <tr><th>Value</th><th>Description</th></tr> 
284            <tr><td><code>intType</code></td><td>Specifies that the animated values are integers</td></tr> 
285            <tr><td><code>floatType</code> (default)</td><td>Specifies that the animated values are floats</td></tr> 
286          </table> 
287        </dd>
288
289      </dl>
290    </dd>
291  </dl>
292
293</dd> <!-- end  elements and attributes -->
294
295<dt>example:</dt>
296<dd>
297  <pp>XML file saved at <code>res/animator/property_animator.xml</code>:</p>
298<pre>
299&lt;set android:ordering="sequentially"&gt;
300    &lt;set&gt;
301        &lt;objectAnimator
302            android:propertyName="x"
303            android:duration="500"
304            android:valueTo="400"
305            android:valueType="intType"/&gt;
306        &lt;objectAnimator
307            android:propertyName="y"
308            android:duration="500"
309            android:valueTo="300"
310            android:valueType="intType"/&gt;
311    &lt;/set&gt;
312    &lt;objectAnimator
313        android:propertyName="alpha"
314        android:duration="500"
315        android:valueTo="1f"/&gt;
316&lt;/set&gt;
317</pre>
318  <p>In order to run this animation, you must inflate the XML resources in your code to an {@link
319  android.animation.AnimatorSet} object, and then set the target objects for all of the animations
320  before starting the animation set. Calling {@link android.animation.AnimatorSet#setTarget
321  setTarget()} sets a single target object for all children of the {@link
322  android.animation.AnimatorSet} as a convenience. The following code shows how to do this:</p>
323  
324<pre>
325AnimatorSet set = (AnimatorSet) AnimatorInflater.loadAnimator(myContext,
326    R.anim.property_animator);
327set.setTarget(myObject);
328set.start();
329</pre>
330
331
332</dd> <!-- end example -->
333
334<dt>see also:</dt>
335<dd>
336<ul>
337  <li><a href="{@docRoot}guide/topics/graphics/prop-animation.html">Property Animation</a></li>
338  <li><a href="{@docRoot}resources/samples/ApiDemos/src/com/example/android/apis/animation/index.html">API Demos</a> for examples
339  on how to use the property animation system.</li>
340</ul>
341</dd>
342
343</dl>
344
345<h2 id="View">View Animation</h2>
346The view animation framework supports both tween and frame by frame animations, which can both be declared
347in XML. The following sections describe how to use both methods.
348
349<h3 id="Tween">Tween animation</h3>
350
351<p>An animation defined in XML that performs transitions such as rotating,
352fading, moving, and stretching on a graphic.
353</p>
354
355<dl class="xml">
356
357<dt>file location:</dt>
358<dd><code>res/anim/<em>filename</em>.xml</code><br/>
359The filename will be used as the resource ID.</dd>
360
361<dt>compiled resource datatype:</dt>
362<dd>Resource pointer to an {@link android.view.animation.Animation}.</dd>
363
364<dt>resource reference:</dt>
365<dd>
366In Java: <code>R.anim.<em>filename</em></code><br/>
367In XML: <code>@[<em>package</em>:]anim/<em>filename</em></code>
368</dd>
369
370<dt>syntax:</dt>
371<dd>
372<pre class="stx">
373&lt;?xml version="1.0" encoding="utf-8"?>
374&lt;<a href="#set-element">set</a> xmlns:android="http://schemas.android.com/apk/res/android"
375    android:interpolator="@[package:]anim/<em>interpolator_resource</em>"
376    android:shareInterpolator=["true" | "false"] &gt;
377    &lt;<a href="#alpha-element">alpha</a>
378        android:fromAlpha="<em>float</em>"
379        android:toAlpha="<em>float</em>" /&gt;
380    &lt;<a href="#scale-element">scale</a>
381        android:fromXScale="<em>float</em>"
382        android:toXScale="<em>float</em>"
383        android:fromYScale="<em>float</em>"
384        android:toYScale="<em>float</em>"
385        android:pivotX="<em>float</em>"
386        android:pivotY="<em>float</em>" /&gt;
387    &lt;<a href="#translate-element">translate</a>
388        android:fromXDelta="<em>float</em>"
389        android:toXDelta="<em>float</em>"
390        android:fromYDelta="<em>float</em>"
391        android:toYDelta="<em>float</em>" /&gt;
392    &lt;<a href="#rotate-element">rotate</a>
393        android:fromDegrees="<em>float</em>"
394        android:toDegrees="<em>float</em>"
395        android:pivotX="<em>float</em>"
396        android:pivotY="<em>float</em>" /&gt;
397    &lt;<a href="#set-element">set</a>&gt;
398        ...
399    &lt;/set&gt;
400&lt;/set&gt;
401</pre>
402
403<p>The file must have a single root element: either an
404<code>&lt;alpha&gt;</code>, <code>&lt;scale&gt;</code>, <code>&lt;translate&gt;</code>,
405<code>&lt;rotate&gt;</code>, or <code>&lt;set&gt;</code> element that holds
406a group (or groups) of other animation elements (even nested <code>&lt;set&gt;</code> elements).
407</p>
408</dd>
409
410<dt>elements:</dt>
411<dd>
412<dl class="tag-list">
413  <dt id="set-element"><code>&lt;set&gt;</code></dt>
414    <dd>A container that holds other animation elements
415(<code>&lt;alpha&gt;</code>, <code>&lt;scale&gt;</code>, <code>&lt;translate&gt;</code>,
416<code>&lt;rotate&gt;</code>) or other <code>&lt;set&gt;</code> elements. Represents an {@link
417android.view.animation.AnimationSet}.
418      <p class="caps">attributes:</p>
419      <dl class="atn-list">
420        <dt><code>android:interpolator</code></dt>
421          <dd><em>Interpolator resource</em>.
422          An {@link android.view.animation.Interpolator} to apply on the animation.
423          The value must be a reference to a resource that specifies an interpolator
424          (not an interpolator class name). There are default interpolator
425          resources available from the platform or you can create your own interpolator resource.
426          See the discussion below for more about <a href="#Interpolators">Interpolators</a>.</dd>
427        <dt><code>android:shareInterpolator</code></dt>
428          <dd><em>Boolean</em>. "true" if you want to share the same interpolator among all child
429elements.</dd>
430      </dl>
431    </dd>
432  <dt id="alpha-element"><code>&lt;alpha&gt;</code></dt>
433    <dd>A fade-in or fade-out animation. Represents an {@link
434android.view.animation.AlphaAnimation}.
435      <p class="caps">attributes:</p>
436      <dl class="atn-list">
437        <dt><code>android:fromAlpha</code></dt>
438          <dd><em>Float</em>. Starting opacity offset, where 0.0 is transparent and 1.0
439is opaque.</dd>
440        <dt><code>android:toAlpha</code></dt>
441          <dd><em>Float</em>. Ending opacity offset, where 0.0 is transparent and 1.0
442is opaque.</dd>
443      </dl>
444      <p>For more attributes supported by <code>&lt;alpha&gt;</code>, see the
445{@link android.view.animation.Animation} class reference (of which, all XML attributes are
446inherrited by this element).</p>
447    </dd>
448  <dt id="scale-element"><code>&lt;scale&gt;</code></dt>
449    <dd>A resizing animation. You can specify the center point of the image from which it grows
450outward (or inward) by specifying {@code pivotX} and {@code pivotY}. For example, if these values
451are 0, 0 (top-left corner), all growth will be down and to the right. Represents a {@link
452android.view.animation.ScaleAnimation}.
453      <p class="caps">attributes:</p>
454      <dl class="atn-list">
455        <dt><code>android:fromXScale</code></dt>
456          <dd><em>Float</em>. Starting X size offset, where 1.0 is no change.</dd>
457        <dt><code>android:toXScale</code></dt>
458          <dd><em>Float</em>. Ending X size offset, where 1.0 is no change.</dd>
459        <dt><code>android:fromYScale</code></dt>
460          <dd><em>Float</em>. Starting Y size offset, where 1.0 is no change.</dd>
461        <dt><code>android:toYScale</code></dt>
462          <dd><em>Float</em>. Ending Y size offset, where 1.0 is no change.</dd>
463        <dt><code>android:pivotX</code></dt>
464          <dd><em>Float</em>. The X coordinate to remain fixed when the object is scaled.</dd>
465        <dt><code>android:pivotY</code></dt>
466          <dd><em>Float</em>. The Y coordinate to remain fixed when the object is scaled.</dd>
467      </dl>
468      <p>For more attributes supported by <code>&lt;scale&gt;</code>, see the
469{@link android.view.animation.Animation} class reference (of which, all XML attributes are
470inherrited by this element).</p>
471    </dd>
472  <dt id="translate-element"><code>&lt;translate&gt;</code></dt>
473    <dd>A vertical and/or horizontal motion. Supports the following attributes in any of
474the following three formats: values from -100 to 100 ending with "%", indicating a percentage
475relative to itself; values from -100 to 100 ending in "%p", indicating a percentage relative to its
476parent; a float value with no suffix, indicating an absolute value. Represents a {@link
477android.view.animation.TranslateAnimation}.
478      <p class="caps">attributes:</p>
479      <dl class="atn-list">
480        <dt><code>android:fromXDelta</code></dt>
481          <dd><em>Float or percentage</em>. Starting X offset. Expressed either: in pixels relative
482to the normal position (such as {@code "5"}), in percentage relative to the element width (such as
483{@code "5%"}), or in percentage relative to the parent width (such as {@code "5%p"}).</dd>
484        <dt><code>android:toXDelta</code></dt>
485          <dd><em>Float or percentage</em>. Ending X offset. Expressed either: in pixels relative
486to the normal position (such as {@code "5"}), in percentage relative to the element width (such as
487{@code "5%"}), or in percentage relative to the parent width (such as {@code "5%p"}).</dd>
488        <dt><code>android:fromYDelta</code></dt>
489          <dd><em>Float or percentage</em>. Starting Y offset. Expressed either: in pixels relative
490to the normal position (such as {@code "5"}), in percentage relative to the element height (such as
491{@code "5%"}), or in percentage relative to the parent height (such as {@code "5%p"}).</dd>
492        <dt><code>android:toYDelta</code></dt>
493          <dd><em>Float or percentage</em>. Ending Y offset. Expressed either: in pixels relative
494to the normal position (such as {@code "5"}), in percentage relative to the element height (such as
495{@code "5%"}), or in percentage relative to the parent height (such as {@code "5%p"}).</dd>
496      </dl>
497      <p>For more attributes supported by <code>&lt;translate&gt;</code>, see the
498{@link android.view.animation.Animation} class reference (of which, all XML attributes are
499inherrited by this element).</p>
500    </dd>
501  <dt id="rotate-element"><code>&lt;rotate&gt;</code></dt>
502    <dd>A rotation animation. Represents a {@link android.view.animation.RotateAnimation}.
503      <p class="caps">attributes:</p>
504      <dl class="atn-list">
505        <dt><code>android:fromDegrees</code></dt>
506          <dd><em>Float</em>. Starting angular position, in degrees.</dd>
507        <dt><code>android:toDegrees</code></dt>
508          <dd><em>Float</em>. Ending angular position, in degrees.</dd>
509        <dt><code>android:pivotX</code></dt>
510          <dd><em>Float or percentage</em>. The X coordinate of the center of rotation. Expressed
511either: in pixels relative to the object's left edge (such as {@code "5"}), in percentage relative
512to the object's left edge (such as {@code "5%"}), or in percentage relative to the parent
513container's left edge (such as {@code "5%p"}).</dd>
514        <dt><code>android:pivotY</code></dt>
515          <dd><em>Float or percentage</em>. The Y coordinate of the center of rotation. Expressed
516either: in pixels relative to the object's top edge (such as {@code "5"}), in percentage relative
517to the object's top edge (such as {@code "5%"}), or in percentage relative to the parent
518container's top edge (such as {@code "5%p"}).</dd>
519        </dl>
520      <p>For more attributes supported by <code>&lt;rotate&gt;</code>, see the
521{@link android.view.animation.Animation} class reference (of which, all XML attributes are
522inherrited by this element).</p>
523    </dd>
524</dl>
525</dd> <!-- end  elements and attributes -->
526
527<dt>example:</dt>
528<dd>
529  <pp>XML file saved at <code>res/anim/hyperspace_jump.xml</code>:</p>
530<pre>
531&lt;set xmlns:android="http://schemas.android.com/apk/res/android"
532    android:shareInterpolator="false">
533    &lt;scale
534        android:interpolator="@android:anim/accelerate_decelerate_interpolator"
535        android:fromXScale="1.0"
536        android:toXScale="1.4"
537        android:fromYScale="1.0"
538        android:toYScale="0.6"
539        android:pivotX="50%"
540        android:pivotY="50%"
541        android:fillAfter="false"
542        android:duration="700" />
543    &lt;set
544        android:interpolator="@android:anim/accelerate_interpolator"
545        android:startOffset="700">
546        &lt;scale
547            android:fromXScale="1.4"
548            android:toXScale="0.0"
549            android:fromYScale="0.6"
550            android:toYScale="0.0"
551            android:pivotX="50%"
552            android:pivotY="50%"
553            android:duration="400" />
554        &lt;rotate
555            android:fromDegrees="0"
556            android:toDegrees="-45"
557            android:toYScale="0.0"
558            android:pivotX="50%"
559            android:pivotY="50%"
560            android:duration="400" />
561    &lt;/set>
562&lt;/set>
563</pre>
564  <p>This application code will apply the animation to an {@link android.widget.ImageView} and
565start the animation:</p>
566<pre>
567ImageView image = (ImageView) findViewById(R.id.image);
568Animation hyperspaceJump = AnimationUtils.{@link android.view.animation.AnimationUtils#loadAnimation(Context,int) loadAnimation}(this, R.anim.hyperspace_jump);
569image.{@link android.view.View#startAnimation(Animation) startAnimation}(hyperspaceJump);
570</pre>
571</dd> <!-- end example -->
572
573
574<dt>see also:</dt>
575<dd>
576<ul>
577  <li><a href="{@docRoot}guide/topics/graphics/view-animation.html#tween-animation">2D
578Graphics: Tween Animation</a></li>
579</ul>
580</dd>
581
582</dl>
583
584<h4 id="Interpolators">Interpolators</h4>
585
586<p>An interpolator is an animation modifier defined in XML that affects the rate of change in an
587animation. This allows your existing animation effects to be accelerated, decelerated, repeated,
588bounced, etc.</p>
589
590<p>An interpolator is applied to an animation element with the {@code android:interpolator}
591attribute, the value of which is a reference to an interpolator resource.</p>
592
593<p>All interpolators available in Android are subclasses of the {@link
594android.view.animation.Interpolator} class. For each interpolator class, Android
595includes a public resource you can reference in order to apply the interpolator to an animation
596using the {@code android:interpolator} attribute.
597The following table specifies the resource to use for each interpolator:</p>
598
599<table>
600  <tr><th>Interpolator class</th><th>Resource ID</th></tr>
601  <tr>
602    <td>{@link android.view.animation.AccelerateDecelerateInterpolator}</td>
603    <td>{@code @android:anim/accelerate_decelerate_interpolator}</td>
604  </tr>
605  <tr>
606    <td>{@link android.view.animation.AccelerateInterpolator}</td>
607    <td>{@code @android:anim/accelerate_interpolator}</td>
608  </tr>
609  <tr>
610    <td>{@link android.view.animation.AnticipateInterpolator}</td>
611    <td>{@code @android:anim/anticipate_interpolator}</td>
612  </tr>
613  <tr>
614    <td>{@link android.view.animation.AnticipateOvershootInterpolator}</td>
615    <td>{@code @android:anim/anticipate_overshoot_interpolator}</td>
616  </tr>
617  <tr>
618    <td>{@link android.view.animation.BounceInterpolator}</td>
619    <td>{@code @android:anim/bounce_interpolator}</td>
620  </tr>
621  <tr>
622    <td>{@link android.view.animation.CycleInterpolator}</td>
623    <td>{@code @android:anim/cycle_interpolator}</td>
624  </tr>
625  <tr>
626    <td>{@link android.view.animation.DecelerateInterpolator}</td>
627    <td>{@code @android:anim/decelerate_interpolator}</td>
628  </tr>
629  <tr>
630    <td>{@link android.view.animation.LinearInterpolator}</td>
631    <td>{@code @android:anim/linear_interpolator}</td>
632  </tr>
633  <tr>
634    <td>{@link android.view.animation.OvershootInterpolator}</td>
635    <td>{@code @android:anim/overshoot_interpolator}</td>
636  </tr>
637</table>
638
639<p>Here's how you can apply one of these with the {@code android:interpolator} attribute:</p>
640<pre>
641&lt;set android:interpolator="@android:anim/accelerate_interpolator"&gt;
642    ...
643&lt;/set&gt;
644</pre>
645
646
647<h4>Custom interpolators</h4>
648
649<p>If you're not satisfied with the interpolators provided by the platform (listed in the
650table above), you can create a custom interpolator resource with modified attributes.
651For example, you can adjust the rate of
652acceleration for the {@link android.view.animation.AnticipateInterpolator}, or adjust the number of
653cycles for the {@link android.view.animation.CycleInterpolator}. In order to do so, you need to
654create your own interpolator resource in an XML file.
655</p>
656
657<dl class="xml">
658
659<dt>file location:</dt>
660<dd><code>res/anim/<em>filename</em>.xml</code><br/>
661The filename will be used as the resource ID.</dd>
662
663<dt>compiled resource datatype:</dt>
664<dd>Resource pointer to the corresponding interpolator object.</dd>
665
666<dt>resource reference:</dt>
667<dd>
668In XML: <code>@[<em>package</em>:]anim/<em>filename</em></code>
669</dd>
670
671<dt>syntax:</dt>
672<dd>
673<pre class="stx">
674&lt;?xml version="1.0" encoding="utf-8"?>
675&lt;<em>InterpolatorName</em> xmlns:android="http://schemas.android.com/apk/res/android"
676    android:<em>attribute_name</em>="<em>value</em>"
677    />
678</pre>
679<p>If you don't apply any attributes, then your interpolator will function exactly the same as
680those provided by the platform (listed in the table above).</p>
681</dd>
682
683<dt>elements:</dt>
684<dd>Notice that each {@link android.view.animation.Interpolator} implementation, when
685defined in XML, begins its name in lowercase.</p>
686
687<dl class="tag-list">
688  <dt><code>&lt;accelerateDecelerateInterpolator&gt;</code></dt>
689    <dd>The rate of change starts and ends slowly but accelerates through the
690middle. <p>No attributes.</p></dd>
691  <dt><code>&lt;accelerateInterpolator&gt;</code></dt>
692    <dd>The rate of change starts out slowly, then accelerates.
693      <p class="caps">attributes:</p>
694      <dl class="atn-list">
695        <dt><code>android:factor</code></dt>
696        <dd><em>Float</em>. The acceleration rate (default is 1).</dd>
697      </dl>
698    </dd>
699  <dt><code>&lt;anticipateInterpolator&gt;</code></dt>
700    <dd>The change starts backward then flings forward.
701      <p class="caps">attributes:</p>
702      <dl class="atn-list">
703        <dt><code>android:tension</code></dt>
704        <dd><em>Float</em>. The amount of tension to apply (default is 2).</dd>
705      </dl>
706    </dd>
707  <dt><code>&lt;anticipateOvershootInterpolator&gt;</code></dt>
708    <dd>The change starts backward, flings forward and overshoots the target value, then
709settles at the final value.
710      <p class="caps">attributes:</p>
711      <dl class="atn-list">
712        <dt><code>android:tension</code></dt>
713        <dd><em>Float</em>. The amount of tension to apply (default is 2).</dd>
714        <dt><code>android:extraTension</code></dt>
715        <dd><em>Float</em>. The amount by which to multiply the tension (default is
716          1.5).</dd>
717      </dl>
718    </dd>
719  <dt><code>&lt;bounceInterpolator&gt;</code></dt>
720    <dd>The change bounces at the end. <p>No attributes</p></dd>
721  <dt><code>&lt;cycleInterpolator&gt;</code></dt>
722    <dd>Repeats the animation for a specified number of cycles. The rate of change follows a
723sinusoidal pattern.
724      <p class="caps">attributes:</p>
725      <dl class="atn-list">
726        <dt><code>android:cycles</code></dt>
727        <dd><em>Integer</em>. The number of cycles (default is 1).</dd>
728      </dl>
729    </dd>
730  <dt><code>&lt;decelerateInterpolator&gt;</code></dt>
731    <dd>The rate of change starts out quickly, then decelerates.
732      <p class="caps">attributes:</p>
733      <dl class="atn-list">
734        <dt><code>android:factor</code></dt>
735        <dd><em>Float</em>. The deceleration rate (default is 1).</dd>
736      </dl>
737    </dd>
738  <dt><code>&lt;linearInterpolator&gt;</code></dt>
739    <dd>The rate of change is constant. <p>No attributes.</p></dd>
740  <dt><code>&lt;overshootInterpolator&gt;</code></dt>
741    <dd>The change flings forward and overshoots the last value, then comes back.
742      <p class="caps">attributes:</p>
743      <dl class="atn-list">
744        <dt><code>android:tension</code></dt>
745        <dd><em>Float</em>. The amount of tension to apply (default is 2).</dd>
746      </dl>
747    </dd>
748</dl>
749
750<dt>example:</dt>
751<dd>
752  <p>XML file saved at <code>res/anim/my_overshoot_interpolator.xml</code>:</p>
753<pre>
754&lt;?xml version="1.0" encoding="utf-8"?>
755&lt;overshootInterpolator xmlns:android="http://schemas.android.com/apk/res/android"
756    android:tension="7.0"
757    />
758</pre>
759  <p>This animation XML will apply the interpolator:</p>
760<pre>
761&lt;scale xmlns:android="http://schemas.android.com/apk/res/android"
762    android:interpolator="@anim/my_overshoot_interpolator"
763    android:fromXScale="1.0"
764    android:toXScale="3.0"
765    android:fromYScale="1.0"
766    android:toYScale="3.0"
767    android:pivotX="50%"
768    android:pivotY="50%"
769    android:duration="700" />
770</pre>
771</dd>
772</dl>
773
774
775<h3 id="Frame">Frame animation</h3>
776
777<p>An animation defined in XML that shows a sequence of images in order (like a film).
778</p>
779
780
781<dl class="xml">
782
783<dt>file location:</dt>
784<dd><code>res/drawable/<em>filename</em>.xml</code><br/>
785The filename will be used as the resource ID.</dd>
786
787<dt>compiled resource datatype:</dt>
788<dd>Resource pointer to an {@link android.graphics.drawable.AnimationDrawable}.</dd>
789
790<dt>resource reference:</dt>
791<dd>
792In Java: <code>R.drawable.<em>filename</em></code><br/>
793In XML: <code>@[<em>package</em>:]drawable.<em>filename</em></code>
794</dd>
795
796<dt>syntax:</dt>
797<dd>
798<pre class="stx">
799&lt;?xml version="1.0" encoding="utf-8"?>
800&lt;<a href="#animation-list-element">animation-list</a> xmlns:android="http://schemas.android.com/apk/res/android"
801    android:oneshot=["true" | "false"] >
802    &lt;<a href="#item-element">item</a>
803        android:drawable="@[package:]drawable/<em>drawable_resource_name</em>"
804        android:duration="<em>integer</em>" />
805&lt;/animation-list>
806</pre>
807</dd>
808
809<dt>elements:</dt>
810<dd>
811<dl class="tag-list">
812<dt id="animation-list-element"><code>&lt;animation-list&gt;</code></dt>
813  <dd><strong>Required</strong>. This must be the root element. Contains one or more
814<code>&lt;item&gt;</code> elements.
815  <p class="caps">attributes:</p>
816    <dl class="atn-list">
817      <dt><code>android:oneshot</code></dt>
818      <dd><em>Boolean</em>. "true" if you want to perform the animation once; "false" to loop the
819animation.</dd>
820    </dl>
821  </dd>
822<dt id="item-element"><code>&lt;item&gt;</code></dt>
823  <dd>A single frame of animation. Must be a child of a <code>&lt;animation-list&gt;</code> element.
824  <p class="caps">attributes:</p>
825    <dl class="atn-list">
826      <dt><code>android:drawable</code></dt>
827      <dd><em>Drawable resource</em>. The drawable to use for this frame.</dd>
828      <dt><code>android:duration</code></dt>
829      <dd><em>Integer</em>. The duration to show this frame, in milliseconds.</dd>
830    </dl>
831  </dd>
832</dl>
833</dd> <!-- end  elements and attributes -->
834
835<dt>example:</dt>
836<dd>
837  <dl>
838    <dt>XML file saved at <code>res/anim/rocket.xml</code>:</dt>
839    <dd>
840<pre>
841&lt;?xml version="1.0" encoding="utf-8"?>
842&lt;animation-list xmlns:android="http://schemas.android.com/apk/res/android"
843    android:oneshot="false">
844    &lt;item android:drawable="@drawable/rocket_thrust1" android:duration="200" />
845    &lt;item android:drawable="@drawable/rocket_thrust2" android:duration="200" />
846    &lt;item android:drawable="@drawable/rocket_thrust3" android:duration="200" />
847&lt;/animation-list>
848</pre>
849    </dd>
850
851    <dt>This application code will set the animation as the background for a View,
852      then play the animation:</dt>
853    <dd>
854<pre>
855ImageView rocketImage = (ImageView) findViewById(R.id.rocket_image);
856rocketImage.{@link android.view.View#setBackgroundResource(int) setBackgroundResource}(R.drawable.rocket_thrust);
857
858rocketAnimation = (AnimationDrawable) rocketImage.{@link android.view.View#getBackground()};
859rocketAnimation.{@link android.graphics.drawable.AnimationDrawable#start()};
860</pre>
861    </dd>
862
863  </dl>
864</dd> <!-- end example -->
865
866<dt>see also:</dt>
867<dd>
868<ul>
869  <li><a href="{@docRoot}guide/topics/graphics/view-animation.html#frame-animation">2D
870Graphics: Frame Animation</a></li>
871</ul>
872</dd>
873</dl>
874