animation-resource.jd revision 218284528bfeebe829633c6977d5fbbbfd144fcf
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>See also</h2>
9    <ol>
10      <li><a href="{@docRoot}guide/topics/graphics/2d-graphics.html#tween-animation">2D
11Graphics</a></li>
12    </ol>
13  </div>
14</div>
15
16
17<p>An animation resource can define one of two types of animations:</p>
18<dl>
19  <dt><a href="#Tween">Tween Animation</a></dt>
20  <dd>Creates an animation by performing a series of transformations on a single image.
21    An {@link android.view.animation.Animation}.</dd>
22  <dt><a href="#Frame">Frame Animation</a></dt>
23  <dd>Creates an animation by showing a sequence of images in order.
24    An {@link android.graphics.drawable.AnimationDrawable}.</dd>
25</dl>
26
27
28
29<h2 id="Tween">Tween Animation</h2>
30
31<p>An animation defined in XML that performs transitions such as rotating,
32fading, moving, and stretching on a graphic.
33</p>
34
35<dl class="xml">
36
37<dt>file location:</dt>
38<dd><code>res/anim/<em>filename</em>.xml</code><br/>
39The filename will be used as the resource ID.</dd>
40
41<dt>compiled resource datatype:</dt>
42<dd>Resource pointer to an {@link android.view.animation.Animation}.</dd>
43
44<dt>resource reference:</dt>
45<dd>
46In Java: <code>R.anim.<em>filename</em></code><br/>
47In XML: <code>@[<em>package</em>:]anim/<em>filename</em></code>
48</dd>
49
50<dt>syntax:</dt>
51<dd>
52<pre class="stx">
53&lt;?xml version="1.0" encoding="utf-8"?>
54&lt;<a href="#set-element">set</a> xmlns:android="http://schemas.android.com/apk/res/android"
55    android:interpolator="@[package:]anim/<em>interpolator_resource</em>"
56    android:shareInterpolator=["true" | "false"] &gt;
57    &lt;<a href="#alpha-element">alpha</a>
58        android:fromAlpha="<em>float</em>"
59        android:toAlpha="<em>float</em>" /&gt;
60    &lt;<a href="#scale-element">scale</a>
61        android:fromXScale="<em>float</em>"
62        android:toXScale="<em>float</em>"
63        android:fromYScale="<em>float</em>"
64        android:toYScale="<em>float</em>"
65        android:pivotX="<em>float</em>"
66        android:pivotY="<em>float</em>" /&gt;
67    &lt;<a href="#translate-element">translate</a>
68        android:fromXDelta="<em>float</em>"
69        android:toXDelta="<em>float</em>"
70        android:fromYDelta="<em>float</em>"
71        android:toYDelta="<em>float</em>" /&gt;
72    &lt;<a href="#rotate-element">rotate</a>
73        android:fromDegrees="<em>float</em>"
74        android:toDegrees="<em>float</em>"
75        android:pivotX="<em>float</em>"
76        android:pivotY="<em>float</em>" /&gt;
77    &lt;<a href="#set-element">set</a>&gt;
78        ...
79    &lt;/set&gt;
80&lt;/set&gt;
81</pre>
82
83<p>The file must have a single root element: either an
84<code>&lt;alpha&gt;</code>, <code>&lt;scale&gt;</code>, <code>&lt;translate&gt;</code>,
85<code>&lt;rotate&gt;</code>, or <code>&lt;set&gt;</code> element that holds
86a group (or groups) of other animation elements (even nested <code>&lt;set&gt;</code> elements).
87</p>
88</dd>
89
90<dt>elements:</dt>
91<dd>
92<dl class="tag-list">
93  <dt id="set-element"><code>&lt;set&gt;</code></dt>
94    <dd>A container that holds other animation elements
95(<code>&lt;alpha&gt;</code>, <code>&lt;scale&gt;</code>, <code>&lt;translate&gt;</code>,
96<code>&lt;rotate&gt;</code>) or other <code>&lt;set&gt;</code> elements. Represents an {@link
97android.view.animation.AnimationSet}.
98      <p class="caps">attributes:</p>
99      <dl class="atn-list">
100        <dt><code>android:interpolator</code></dt>
101          <dd><em>Interpolator resource</em>.
102          An {@link android.view.animation.Interpolator} to apply on the animation.
103          The value must be a reference to a resource that specifies an interpolator
104          (not an interpolator class name). There are default interpolator
105          resources available from the platform or you can create your own interpolator resource.
106          See the discussion below for more about <a href="#Interpolators">Interpolators</a>.</dd>
107        <dt><code>android:shareInterpolator</code></dt>
108          <dd><em>Boolean</em>. "true" if you want to share the same interpolator among all child
109elements.</dd>
110      </dl>
111    </dd>
112  <dt id="alpha-element"><code>&lt;alpha&gt;</code></dt>
113    <dd>A fade-in or fade-out animation. Represents an {@link
114android.view.animation.AlphaAnimation}.
115      <p class="caps">attributes:</p>
116      <dl class="atn-list">
117        <dt><code>android:fromAlpha</code></dt>
118          <dd><em>Float</em>. Starting opacity offset, where 0.0 is transparent and 1.0
119is opaque.</dd>
120        <dt><code>android:toAlpha</code></dt>
121          <dd><em>Float</em>. Ending opacity offset, where 0.0 is transparent and 1.0
122is opaque.</dd>
123      </dl>
124      <p>For more attributes supported by <code>&lt;alpha&gt;</code>, see the
125{@link android.view.animation.Animation} class reference (of which, all XML attributes are
126inherrited by this element).</p>
127    </dd>
128  <dt id="scale-element"><code>&lt;scale&gt;</code></dt>
129    <dd>A resizing animation. You can specify the center point of the image from which it grows
130outward (or inward) by specifying {@code pivotX} and {@code pivotY}. For example, if these values
131are 0, 0 (top-left corner), all growth will be down and to the right. Represents a {@link
132android.view.animation.ScaleAnimation}.
133      <p class="caps">attributes:</p>
134      <dl class="atn-list">
135        <dt><code>android:fromXScale</code></dt>
136          <dd><em>Float</em>. Starting X size offset, where 1.0 is no change.</dd>
137        <dt><code>android:toXScale</code></dt>
138          <dd><em>Float</em>. Ending X size offset, where 1.0 is no change.</dd>
139        <dt><code>android:fromYScale</code></dt>
140          <dd><em>Float</em>. Starting Y size offset, where 1.0 is no change.</dd>
141        <dt><code>android:toYScale</code></dt>
142          <dd><em>Float</em>. Ending Y size offset, where 1.0 is no change.</dd>
143        <dt><code>android:pivotX</code></dt>
144          <dd><em>Float</em>. The X coordinate to remain fixed when the object is scaled.</dd>
145        <dt><code>android:pivotY</code></dt>
146          <dd><em>Float</em>. The Y coordinate to remain fixed when the object is scaled.</dd>
147      </dl>
148      <p>For more attributes supported by <code>&lt;scale&gt;</code>, see the
149{@link android.view.animation.Animation} class reference (of which, all XML attributes are
150inherrited by this element).</p>
151    </dd>
152  <dt id="translate-element"><code>&lt;translate&gt;</code></dt>
153    <dd>A vertical and/or horizontal motion. Supports the following attributes in any of
154the following three formats: values from -100 to 100 ending with "%", indicating a percentage
155relative to itself; values from -100 to 100 ending in "%p", indicating a percentage relative to its
156parent; a float value with no suffix, indicating an absolute value. Represents a {@link
157android.view.animation.TranslateAnimation}.
158      <p class="caps">attributes:</p>
159      <dl class="atn-list">
160        <dt><code>android:fromXDelta</code></dt>
161          <dd><em>Float or percentage</em>. Starting X offset. Expressed either: in pixels relative
162to the normal position (such as {@code "5"}), in percentage relative to the element width (such as
163{@code "5%"}), or in percentage relative to the parent width (such as {@code "5%p"}).</dd>
164        <dt><code>android:toXDelta</code></dt>
165          <dd><em>Float or percentage</em>. Ending X offset. Expressed either: in pixels relative
166to the normal position (such as {@code "5"}), in percentage relative to the element width (such as
167{@code "5%"}), or in percentage relative to the parent width (such as {@code "5%p"}).</dd>
168        <dt><code>android:fromYDelta</code></dt>
169          <dd><em>Float or percentage</em>. Starting Y offset. Expressed either: in pixels relative
170to the normal position (such as {@code "5"}), in percentage relative to the element height (such as
171{@code "5%"}), or in percentage relative to the parent height (such as {@code "5%p"}).</dd>
172        <dt><code>android:toYDelta</code></dt>
173          <dd><em>Float or percentage</em>. Ending Y offset. Expressed either: in pixels relative
174to the normal position (such as {@code "5"}), in percentage relative to the element height (such as
175{@code "5%"}), or in percentage relative to the parent height (such as {@code "5%p"}).</dd>
176      </dl>
177      <p>For more attributes supported by <code>&lt;translate&gt;</code>, see the
178{@link android.view.animation.Animation} class reference (of which, all XML attributes are
179inherrited by this element).</p>
180    </dd>
181  <dt id="rotate-element"><code>&lt;rotate&gt;</code></dt>
182    <dd>A rotation animation. Represents a {@link android.view.animation.RotateAnimation}.
183      <p class="caps">attributes:</p>
184      <dl class="atn-list">
185        <dt><code>android:fromDegrees</code></dt>
186          <dd><em>Float</em>. Starting angular position, in degrees.</dd>
187        <dt><code>android:toDegrees</code></dt>
188          <dd><em>Float</em>. Ending angular position, in degrees.</dd>
189        <dt><code>android:pivotX</code></dt>
190          <dd><em>Float or percentage</em>. The X coordinate of the center of rotation. Expressed
191either: in pixels relative to the object's left edge (such as {@code "5"}), in percentage relative
192to the object's left edge (such as {@code "5%"}), or in percentage relative to the parent
193container's left edge (such as {@code "5%p"}).</dd>
194        <dt><code>android:pivotY</code></dt>
195          <dd><em>Float or percentage</em>. The Y coordinate of the center of rotation. Expressed
196either: in pixels relative to the object's top edge (such as {@code "5"}), in percentage relative
197to the object's top edge (such as {@code "5%"}), or in percentage relative to the parent
198container's top edge (such as {@code "5%p"}).</dd>
199        </dl>
200      <p>For more attributes supported by <code>&lt;rotate&gt;</code>, see the
201{@link android.view.animation.Animation} class reference (of which, all XML attributes are
202inherrited by this element).</p>
203    </dd>
204</dl>
205</dd> <!-- end  elements and attributes -->
206
207<dt>example:</dt>
208<dd>
209  <pp>XML file saved at <code>res/anim/hyperspace_jump.xml</code>:</p>
210<pre>
211&lt;set xmlns:android="http://schemas.android.com/apk/res/android"
212    android:shareInterpolator="false">
213    &lt;scale
214        android:interpolator="@android:anim/accelerate_decelerate_interpolator"
215        android:fromXScale="1.0"
216        android:toXScale="1.4"
217        android:fromYScale="1.0"
218        android:toYScale="0.6"
219        android:pivotX="50%"
220        android:pivotY="50%"
221        android:fillAfter="false"
222        android:duration="700" />
223    &lt;set
224        android:interpolator="@android:anim/accelerate_interpolator"
225        android:startOffset="700">
226        &lt;scale
227            android:fromXScale="1.4"
228            android:toXScale="0.0"
229            android:fromYScale="0.6"
230            android:toYScale="0.0"
231            android:pivotX="50%"
232            android:pivotY="50%"
233            android:duration="400" />
234        &lt;rotate
235            android:fromDegrees="0"
236            android:toDegrees="-45"
237            android:toYScale="0.0"
238            android:pivotX="50%"
239            android:pivotY="50%"
240            android:duration="400" />
241    &lt;/set>
242&lt;/set>
243</pre>
244  <p>This application code will apply the animation to an {@link android.widget.ImageView} and
245start the animation:</p>
246<pre>
247ImageView image = (ImageView) findViewById(R.id.image);
248Animation hyperspaceJump = AnimationUtils.{@link android.view.animation.AnimationUtils#loadAnimation(Context,int) loadAnimation}(this, R.anim.hyperspace_jump);
249image.{@link android.view.View#startAnimation(Animation) startAnimation}(hyperspaceJump);
250</pre>
251</dd> <!-- end example -->
252
253
254<dt>see also:</dt>
255<dd>
256<ul>
257  <li><a href="{@docRoot}guide/topics/graphics/2d-graphics.html#tween-animation">2D
258Graphics: Tween Animation</a></li>
259</ul>
260</dd>
261
262</dl>
263
264
265
266
267
268<h3 id="Interpolators">Interpolators</h3>
269
270<p>An interpolator is an animation modifier defined in XML that affects the rate of change in an
271animation. This allows your existing animation effects to be accelerated, decelerated, repeated,
272bounced, etc.</p>
273
274<p>An interpolator is applied to an animation element with the {@code android:interpolator}
275attribute, the value of which is a reference to an interpolator resource.</p>
276
277<p>All interpolators available in Android are subclasses of the {@link
278android.view.animation.Interpolator} class. For each interpolator class, Android
279includes a public resource you can reference in order to apply the interpolator to an animation
280using the the {@code android:interpolator} attribute.
281The following table specifies the resource to use for each interpolator:</p>
282
283<table>
284  <tr><th>Interpolator class</th><th>Resource ID</th></tr>
285  <tr>
286    <td>{@link android.view.animation.AccelerateDecelerateInterpolator}</td>
287    <td>{@code @android:anim/accelerate_decelerate_interpolator}</td>
288  </tr>
289  <tr>
290    <td>{@link android.view.animation.AccelerateInterpolator}</td>
291    <td>{@code @android:anim/accelerate_interpolator}</td>
292  </tr>
293  <tr>
294    <td>{@link android.view.animation.AnticipateInterpolator}</td>
295    <td>{@code @android:anim/anticipate_interpolator}</td>
296  </tr>
297  <tr>
298    <td>{@link android.view.animation.AnticipateOvershootInterpolator}</td>
299    <td>{@code @android:anim/anticipate_overshoot_interpolator}</td>
300  </tr>
301  <tr>
302    <td>{@link android.view.animation.BounceInterpolator}</td>
303    <td>{@code @android:anim/bounce_interpolator}</td>
304  </tr>
305  <tr>
306    <td>{@link android.view.animation.CycleInterpolator}</td>
307    <td>{@code @android:anim/cycle_interpolator}</td>
308  </tr>
309  <tr>
310    <td>{@link android.view.animation.DecelerateInterpolator}</td>
311    <td>{@code @android:anim/decelerate_interpolator}</td>
312  </tr>
313  <tr>
314    <td>{@link android.view.animation.LinearInterpolator}</td>
315    <td>{@code @android:anim/linear_interpolator}</td>
316  </tr>
317  <tr>
318    <td>{@link android.view.animation.OvershootInterpolator}</td>
319    <td>{@code @android:anim/overshoot_interpolator}</td>
320  </tr>
321</table>
322
323<p>Here's how you can apply one of these with the {@code android:interpolator} attribute:</p>
324<pre>
325&lt;set android:interpolator="@android:anim/accelerate_interpolator"&gt;
326    ...
327&lt;/set&gt;
328</pre>
329
330
331<h4>Custom interpolators</h4>
332
333<p>If you're not satisfied with the interpolators provided by the platform (listed in the
334table above), you can create a custom interpolator resource with modified attributes.
335For example, you can adjust the rate of
336acceleration for the {@link android.view.animation.AnticipateInterpolator}, or adjust the number of
337cycles for the {@link android.view.animation.CycleInterpolator}. In order to do so, you need to
338create your own interpolator resource in an XML file.
339</p>
340
341<dl class="xml">
342
343<dt>file location:</dt>
344<dd><code>res/anim/<em>filename</em>.xml</code><br/>
345The filename will be used as the resource ID.</dd>
346
347<dt>compiled resource datatype:</dt>
348<dd>Resource pointer to the corresponding interpolator object.</dd>
349
350<dt>resource reference:</dt>
351<dd>
352In XML: <code>@[<em>package</em>:]anim/<em>filename</em></code>
353</dd>
354
355<dt>syntax:</dt>
356<dd>
357<pre class="stx">
358&lt;?xml version="1.0" encoding="utf-8"?>
359&lt;<em>InterpolatorName</em> xmlns:android="http://schemas.android.com/apk/res/android"
360    android:<em>attribute_name</em>="<em>value</em>"
361    />
362</pre>
363<p>If you don't apply any attributes, then your interpolator will function exactly the same as
364those provided by the platform (listed in the table above).</p>
365</dd>
366
367<dt>elements:</dt>
368<dd>Notice that each {@link android.view.animation.Interpolator} implementation, when
369defined in XML, begins its name in lowercase.</p>
370
371<dl class="tag-list">
372  <dt><code>&lt;accelerateDecelerateInterpolator&gt;</code></dt>
373    <dd>The rate of change starts and ends slowly but accelerates through the
374middle. <p>No attributes.</p></dd>
375  <dt><code>&lt;accelerateInterpolator&gt;</code></dt>
376    <dd>The rate of change starts out slowly, then accelerates.
377      <p class="caps">attributes:</p>
378      <dl class="atn-list">
379        <dt><code>android:factor</code></dt>
380        <dd><em>Float</em>. The acceleration rate (default is 1).</dd>
381      </dl>
382    </dd>
383  <dt><code>&lt;anticipateInterpolator&gt;</code></dt>
384    <dd>The change starts backward then flings forward.
385      <p class="caps">attributes:</p>
386      <dl class="atn-list">
387        <dt><code>android:tension</code></dt>
388        <dd><em>Float</em>. The amount of tension to apply (default is 2).</dd>
389      </dl>
390    </dd>
391  <dt><code>&lt;anticipateOvershootInterpolator&gt;</code></dt>
392    <dd>The change starts backward, flings forward and overshoots the target value, then
393settles at the final value.
394      <p class="caps">attributes:</p>
395      <dl class="atn-list">
396        <dt><code>android:tension</code></dt>
397        <dd><em>Float</em>. The amount of tension to apply (default is 2).</dd>
398        <dt><code>android:extraTension</code></dt>
399        <dd><em>Float</em>. The amount by which to multiply the tension (default is
400          1.5).</dd>
401      </dl>
402    </dd>
403  <dt><code>&lt;bounceInterpolator&gt;</code></dt>
404    <dd>The change bounces at the end. <p>No attributes</p></dd>
405  <dt><code>&lt;cycleInterpolator&gt;</code></dt>
406    <dd>Repeats the animation for a specified number of cycles. The rate of change follows a
407sinusoidal pattern.
408      <p class="caps">attributes:</p>
409      <dl class="atn-list">
410        <dt><code>android:cycles</code></dt>
411        <dd><em>Integer</em>. The number of cycles (default is 1).</dd>
412      </dl>
413    </dd>
414  <dt><code>&lt;decelerateInterpolator&gt;</code></dt>
415    <dd>The rate of change starts out quickly, then decelerates.
416      <p class="caps">attributes:</p>
417      <dl class="atn-list">
418        <dt><code>android:factor</code></dt>
419        <dd><em>Float</em>. The deceleration rate (default is 1).</dd>
420      </dl>
421    </dd>
422  <dt><code>&lt;linearInterpolator&gt;</code></dt>
423    <dd>The rate of change is constant. <p>No attributes.</p></dd>
424  <dt><code>&lt;overshootInterpolator&gt;</code></dt>
425    <dd>The change flings forward and overshoots the last value, then comes back.
426      <p class="caps">attributes:</p>
427      <dl class="atn-list">
428        <dt><code>android:tension</code></dt>
429        <dd><em>Float</em>. The amount of tension to apply (default is 2).</dd>
430      </dl>
431    </dd>
432</dl>
433
434<dt>example:</dt>
435<dd>
436  <p>XML file saved at <code>res/anim/my_overshoot_interpolator.xml</code>:</p>
437<pre>
438&lt;?xml version="1.0" encoding="utf-8"?>
439&lt;overshootInterpolator xmlns:android="http://schemas.android.com/apk/res/android"
440    android:tension="7.0"
441    />
442</pre>
443  <p>This animation XML will apply the interpolator:</p>
444<pre>
445&lt;scale xmlns:android="http://schemas.android.com/apk/res/android"
446    android:interpolator="@anim/my_overshoot_interpolator"
447    android:fromXScale="1.0"
448    android:toXScale="3.0"
449    android:fromYScale="1.0"
450    android:toYScale="3.0"
451    android:pivotX="50%"
452    android:pivotY="50%"
453    android:duration="700" />
454</pre>
455</dd>
456</dl>
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474<h2 id="Frame">Frame Animation</h2>
475
476<p>An animation defined in XML that shows a sequence of images in order (like a film).
477</p>
478
479
480<dl class="xml">
481
482<dt>file location:</dt>
483<dd><code>res/drawable/<em>filename</em>.xml</code><br/>
484The filename will be used as the resource ID.</dd>
485
486<dt>compiled resource datatype:</dt>
487<dd>Resource pointer to an {@link android.graphics.drawable.AnimationDrawable}.</dd>
488
489<dt>resource reference:</dt>
490<dd>
491In Java: <code>R.drawable.<em>filename</em></code><br/>
492In XML: <code>@[<em>package</em>:]drawable.<em>filename</em></code>
493</dd>
494
495<dt>syntax:</dt>
496<dd>
497<pre class="stx">
498&lt;?xml version="1.0" encoding="utf-8"?>
499&lt;<a href="#animation-list-element">animation-list</a> xmlns:android="http://schemas.android.com/apk/res/android"
500    android:oneshot=["true" | "false"] >
501    &lt;<a href="#item-element">item</a>
502        android:drawable="@[package:]drawable/<em>drawable_resource_name</em>"
503        android:duration="<em>integer</em>" />
504&lt;/animation-list>
505</pre>
506</dd>
507
508<dt>elements:</dt>
509<dd>
510<dl class="tag-list">
511<dt id="animation-list-element"><code>&lt;animation-list&gt;</code></dt>
512  <dd><strong>Required</strong>. This must be the root element. Contains one or more
513<code>&lt;item&gt;</code> elements.
514  <p class="caps">attributes:</p>
515    <dl class="atn-list">
516      <dt><code>android:oneshot</code></dt>
517      <dd><em>Boolean</em>. "true" if you want to perform the animation once; "false" to loop the
518animation.</dd>
519    </dl>
520  </dd>
521<dt id="item-element"><code>&lt;item&gt;</code></dt>
522  <dd>A single frame of animation. Must be a child of a <code>&lt;animation-list&gt;</code> element.
523  <p class="caps">attributes:</p>
524    <dl class="atn-list">
525      <dt><code>android:drawable</code></dt>
526      <dd><em>Drawable resource</em>. The drawable to use for this frame.</dd>
527      <dt><code>android:duration</code></dt>
528      <dd><em>Integer</em>. The duration to show this frame, in milliseconds.</dd>
529    </dl>
530  </dd>
531</dl>
532</dd> <!-- end  elements and attributes -->
533
534<dt>example:</dt>
535<dd>
536  <dl>
537    <dt>XML file saved at <code>res/anim/rocket.xml</code>:</dt>
538    <dd>
539<pre>
540&lt;?xml version="1.0" encoding="utf-8"?>
541&lt;animation-list xmlns:android="http://schemas.android.com/apk/res/android"
542    android:oneshot="false">
543    &lt;item android:drawable="@drawable/rocket_thrust1" android:duration="200" />
544    &lt;item android:drawable="@drawable/rocket_thrust2" android:duration="200" />
545    &lt;item android:drawable="@drawable/rocket_thrust3" android:duration="200" />
546&lt;/animation-list>
547</pre>
548    </dd>
549
550    <dt>This application code will set the animation as the background for a View,
551      then play the animation:</dt>
552    <dd>
553<pre>
554ImageView rocketImage = (ImageView) findViewById(R.id.rocket_image);
555rocketImage.{@link android.view.View#setBackgroundResource(int) setBackgroundResource}(R.drawable.rocket_thrust);
556
557rocketAnimation = (AnimationDrawable) rocketImage.{@link android.view.View#getBackground()};
558rocketAnimation.{@link android.graphics.drawable.AnimationDrawable#start()};
559</pre>
560    </dd>
561
562  </dl>
563</dd> <!-- end example -->
564
565</dl>
566
567
568
569