1 page.title=Animation Resources 2 parent.title=Resource Types 3 parent.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 11 Graphics</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, 32 fading, 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/> 39 The 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> 46 In Java: <code>R.anim.<em>filename</em></code><br/> 47 In XML: <code>@[<em>package</em>:]anim/<em>filename</em></code> 48 </dd> 49 50 <dt>syntax:</dt> 51 <dd> 52 <pre class="stx"> 53 <?xml version="1.0" encoding="utf-8"?> 54 <<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"] > 57 <<a href="#alpha-element">alpha</a> 58 android:fromAlpha="<em>float</em>" 59 android:toAlpha="<em>float</em>" /> 60 <<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>" /> 67 <<a href="#translate-element">translate</a> 68 android:fromX="<em>float</em>" 69 android:toX="<em>float</em>" 70 android:fromY="<em>float</em>" 71 android:toY="<em>float</em>" /> 72 <<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>" /> 77 <<a href="#set-element">set</a>> 78 ... 79 </set> 80 </set> 81 </pre> 82 83 <p>The file must have a single root element: either an 84 <code><alpha></code>, <code><scale></code>, <code><translate></code>, 85 <code><rotate></code>, or <code><set></code> element that holds 86 a group (or groups) of other animation elements (even nested <code><set></code> elements). 87 </p> 88 </dd> 89 90 <dt>elements:</dt> 91 <dd> 92 <dl class="tag-list"> 93 <dt id="set-element"><code><set></code></dt> 94 <dd>A container that holds other animation elements 95 (<code><alpha></code>, <code><scale></code>, <code><translate></code>, 96 <code><rotate></code>) or other <code><set></code> elements. Represents an {@link 97 android.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 109 elements.</dd> 110 </dl> 111 </dd> 112 <dt id="alpha-element"><code><alpha></code></dt> 113 <dd>A fade-in or fade-out animation. Represents an {@link 114 android.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 119 is 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 122 is opaque.</dd> 123 </dl> 124 <p>For more attributes supported by <code><alpha></code>, see the 125 {@link android.view.animation.Animation} class reference (of which, all XML attributes are 126 inherrited by this element).</p> 127 </dd> 128 <dt id="scale-element"><code><scale></code></dt> 129 <dd>A resizing animation. You can specify the center point of the image from which it grows 130 outward (or inward) by specifying {@code pivotX} and {@code pivotY}. For example, if these values 131 are 0, 0 (top-left corner), all growth will be down and to the right. Represents a {@link 132 android.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><scale></code>, see the 149 {@link android.view.animation.Animation} class reference (of which, all XML attributes are 150 inherrited by this element).</p> 151 </dd> 152 <dt id="translate-element"><code><translate></code></dt> 153 <dd>A vertical and/or horizontal motion. Supports the following attributes in any of 154 the following three formats: values from -100 to 100 ending with "%", indicating a percentage 155 relative to itself; values from -100 to 100 ending in "%p", indicating a percentage relative to its 156 parent; a float value with no suffix, indicating an absolute value. Represents a {@link 157 android.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 162 to 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 166 to 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 170 to 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 174 to 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><translate></code>, see the 178 {@link android.view.animation.Animation} class reference (of which, all XML attributes are 179 inherrited by this element).</p> 180 </dd> 181 <dt id="rotate-element"><code><rotate></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 191 either: in pixels relative to the object's left edge (such as {@code "5"}), in percentage relative 192 to the object's left edge (such as {@code "5%"}), or in percentage relative to the parent 193 container'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 196 either: in pixels relative to the object's top edge (such as {@code "5"}), in percentage relative 197 to the object's top edge (such as {@code "5%"}), or in percentage relative to the parent 198 container's top edge (such as {@code "5%p"}).</dd> 199 </dl> 200 <p>For more attributes supported by <code><rotate></code>, see the 201 {@link android.view.animation.Animation} class reference (of which, all XML attributes are 202 inherrited 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 <set xmlns:android="http://schemas.android.com/apk/res/android" 212 android:shareInterpolator="false"> 213 <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 <set 224 android:interpolator="@android:anim/accelerate_interpolator" 225 android:startOffset="700"> 226 <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 <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 </set> 242 </set> 243 </pre> 244 <p>This application code will apply the animation to an {@link android.widget.ImageView} and 245 start the animation:</p> 246 <pre> 247 ImageView image = (ImageView) findViewById(R.id.image); 248 Animation hyperspaceJump = AnimationUtils.{@link android.view.animation.AnimationUtils#loadAnimation(Context,int) loadAnimation}(this, R.anim.hyperspace_jump); 249 image.{@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 258 Graphics: 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 271 animation. This allows your existing animation effects to be accelerated, decelerated, repeated, 272 bounced, etc.</p> 273 274 <p>An interpolator is applied to an animation element with the {@code android:interpolator} 275 attribute, 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 278 android.view.animation.Interpolator} class. For each interpolator class, Android 279 includes a public resource you can reference in order to apply the interpolator to an animation 280 using the the {@code android:interpolator} attribute. 281 The 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 <set android:interpolator="@android:anim/accelerate_interpolator"> 326 ... 327 </set> 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 334 table above), you can create a custom interpolator resource with modified attributes. 335 For example, you can adjust the rate of 336 acceleration for the {@link android.view.animation.AnticipateInterpolator}, or adjust the number of 337 cycles for the {@link android.view.animation.CycleInterpolator}. In order to do so, you need to 338 create 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/> 345 The 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> 352 In XML: <code>@[<em>package</em>:]anim/<em>filename</em></code> 353 </dd> 354 355 <dt>syntax:</dt> 356 <dd> 357 <pre class="stx"> 358 <?xml version="1.0" encoding="utf-8"?> 359 <<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 364 those 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 369 defined in XML, begins its name in lowercase.</p> 370 371 <dl class="tag-list"> 372 <dt><code><accelerateDecelerateInterpolator></code></dt> 373 <dd>The rate of change starts and ends slowly but accelerates through the 374 middle. <p>No attributes.</p></dd> 375 <dt><code><accelerateInterpolator></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><anticipateInterpolator></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><anticipateOvershootInterpolator></code></dt> 392 <dd>The change starts backward, flings forward and overshoots the target value, then 393 settles 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><bounceInterpolator></code></dt> 404 <dd>The change bounces at the end. <p>No attributes</p></dd> 405 <dt><code><cycleInterpolator></code></dt> 406 <dd>Repeats the animation for a specified number of cycles. The rate of change follows a 407 sinusoidal 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><decelerateInterpolator></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><linearInterpolator></code></dt> 423 <dd>The rate of change is constant. <p>No attributes.</p></dd> 424 <dt><code><overshootInterpolator></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 <?xml version="1.0" encoding="utf-8"?> 439 <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 <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/> 484 The 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> 491 In Java: <code>R.drawable.<em>filename</em></code><br/> 492 In XML: <code>@[<em>package</em>:]drawable.<em>filename</em></code> 493 </dd> 494 495 <dt>syntax:</dt> 496 <dd> 497 <pre class="stx"> 498 <?xml version="1.0" encoding="utf-8"?> 499 <<a href="#animation-list-element">animation-list</a> xmlns:android="http://schemas.android.com/apk/res/android" 500 android:oneshot=["true" | "false"] > 501 <<a href="#item-element">item</a> 502 android:drawable="@[package:]drawable/<em>drawable_resource_name</em>" 503 android:duration="<em>integer</em>" /> 504 </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><animation-list></code></dt> 512 <dd><strong>Required</strong>. This must be the root element. Contains one or more 513 <code><item></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 518 animation.</dd> 519 </dl> 520 </dd> 521 <dt id="item-element"><code><item></code></dt> 522 <dd>A single frame of animation. Must be a child of a <code><animation-list></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 <?xml version="1.0" encoding="utf-8"?> 541 <animation-list xmlns:android="http://schemas.android.com/apk/res/android" 542 android:oneshot="false"> 543 <item android:drawable="@drawable/rocket_thrust1" android:duration="200" /> 544 <item android:drawable="@drawable/rocket_thrust2" android:duration="200" /> 545 <item android:drawable="@drawable/rocket_thrust3" android:duration="200" /> 546 </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> 554 ImageView rocketImage = (ImageView) findViewById(R.id.rocket_image); 555 rocketImage.{@link android.view.View#setBackgroundResource(int) setBackgroundResource}(R.drawable.rocket_thrust); 556 557 rocketAnimation = (AnimationDrawable) rocketImage.{@link android.view.View#getBackground()}; 558 rocketAnimation.{@link android.graphics.drawable.AnimationDrawable#start()}; 559 </pre> 560 </dd> 561 562 </dl> 563 </dd> <!-- end example --> 564 565 </dl> 566 567 568 569