1page.title=View Animation 2parent.title=Animation 3parent.link=animation.html 4@jd:body 5 6 7 8 <p>You can use the view animation system to perform tweened animation on Views. Tween animation 9 calculates the animation with information such as the start point, end point, size, rotation, and 10 other common aspects of an animation. 11 </p> 12 13 <p>A tween animation can perform a series of simple transformations (position, size, rotation, 14 and transparency) on the contents of a View object. So, if you have a {@link 15 android.widget.TextView} object, you can move, rotate, grow, or shrink the text. If it has a 16 background image, the background image will be transformed along with the text. The {@link 17 android.view.animation animation package} provides all the classes used in a tween animation.</p> 18 19 <p>A sequence of animation instructions defines the tween animation, defined by either XML or 20 Android code. As with defining a layout, an XML file is recommended because it's more readable, 21 reusable, and swappable than hard-coding the animation. In the example below, we use XML. (To 22 learn more about defining an animation in your application code, instead of XML, refer to the 23 {@link android.view.animation.AnimationSet} class and other {@link 24 android.view.animation.Animation} subclasses.)</p> 25 26 <p>The animation instructions define the transformations that you want to occur, when they will 27 occur, and how long they should take to apply. Transformations can be sequential or simultaneous 28 - for example, you can have the contents of a TextView move from left to right, and then rotate 29 180 degrees, or you can have the text move and rotate simultaneously. Each transformation takes a 30 set of parameters specific for that transformation (starting size and ending size for size 31 change, starting angle and ending angle for rotation, and so on), and also a set of common 32 parameters (for instance, start time and duration). To make several transformations happen 33 simultaneously, give them the same start time; to make them sequential, calculate the start time 34 plus the duration of the preceding transformation.</p> 35 36 <p>The animation XML file belongs in the <code>res/anim/</code> directory of your Android 37 project. The file must have a single root element: this will be either a single 38 <code><alpha></code>, <code><scale></code>, <code><translate></code>, 39 <code><rotate></code>, interpolator element, or <code><set></code> element that holds 40 groups of these elements (which may include another <code><set></code>). By default, all 41 animation instructions are applied simultaneously. To make them occur sequentially, you must 42 specify the <code>startOffset</code> attribute, as shown in the example below.</p> 43 44 <p>The following XML from one of the ApiDemos is used to stretch, then simultaneously spin and 45 rotate a View object.</p> 46 <pre> 47<set android:shareInterpolator="false"> 48 <scale 49 android:interpolator="@android:anim/accelerate_decelerate_interpolator" 50 android:fromXScale="1.0" 51 android:toXScale="1.4" 52 android:fromYScale="1.0" 53 android:toYScale="0.6" 54 android:pivotX="50%" 55 android:pivotY="50%" 56 android:fillAfter="false" 57 android:duration="700" /> 58 <set android:interpolator="@android:anim/decelerate_interpolator"> 59 <scale 60 android:fromXScale="1.4" 61 android:toXScale="0.0" 62 android:fromYScale="0.6" 63 android:toYScale="0.0" 64 android:pivotX="50%" 65 android:pivotY="50%" 66 android:startOffset="700" 67 android:duration="400" 68 android:fillBefore="false" /> 69 <rotate 70 android:fromDegrees="0" 71 android:toDegrees="-45" 72 android:toYScale="0.0" 73 android:pivotX="50%" 74 android:pivotY="50%" 75 android:startOffset="700" 76 android:duration="400" /> 77 </set> 78</set> 79</pre> 80 81 <p>Screen coordinates (not used in this example) are (0,0) at the upper left hand corner, and 82 increase as you go down and to the right.</p> 83 84 <p>Some values, such as pivotX, can be specified relative to the object itself or relative to the 85 parent. Be sure to use the proper format for what you want ("50" for 50% relative to the parent, 86 or "50%" for 50% relative to itself).</p> 87 88 <p>You can determine how a transformation is applied over time by assigning an {@link 89 android.view.animation.Interpolator}. Android includes several Interpolator subclasses that 90 specify various speed curves: for instance, {@link android.view.animation.AccelerateInterpolator} 91 tells a transformation to start slow and speed up. Each one has an attribute value that can be 92 applied in the XML.</p> 93 94 <p>With this XML saved as <code>hyperspace_jump.xml</code> in the <code>res/anim/</code> 95 directory of the project, the following code will reference it and apply it to an {@link 96 android.widget.ImageView} object from the layout.</p> 97 <pre> 98ImageView spaceshipImage = (ImageView) findViewById(R.id.spaceshipImage); 99Animation hyperspaceJumpAnimation = AnimationUtils.loadAnimation(this, R.anim.hyperspace_jump); 100spaceshipImage.startAnimation(hyperspaceJumpAnimation); 101</pre> 102 103 <p>As an alternative to <code>startAnimation()</code>, you can define a starting time for the 104 animation with <code>{@link android.view.animation.Animation#setStartTime(long) 105 Animation.setStartTime()}</code>, then assign the animation to the View with <code>{@link 106 android.view.View#setAnimation(android.view.animation.Animation) View.setAnimation()}</code>.</p> 107 108 <p>For more information on the XML syntax, available tags and attributes, see <a href= 109 "{@docRoot}guide/topics/resources/animation-resource.html">Animation Resources</a>.</p> 110 111 <p class="note"><strong>Note:</strong> Regardless of how your animation may move or resize, the 112 bounds of the View that holds your animation will not automatically adjust to accommodate it. 113 Even so, the animation will still be drawn beyond the bounds of its View and will not be clipped. 114 However, clipping <em>will occur</em> if the animation exceeds the bounds of the parent View.</p> 115 116