Android can perform simple animation on a graphic, or a series of graphics. These include rotations, fading, moving, and stretching.
Source file format: XML file, one resource per file, one root tag with no <?xml> declaration
Resource file location: res/anim/some_file.xml
Compiled resource datatype: Resource pointer to an Animation.
Resource reference name:
Java:R.anim.some_file
XML:@[package:]anim/some_file
Syntax
The file must have a single root element: this will be either a single <alpha>, <scale>, <translate>, <rotate>, interpolator element, or <set> element that holds groups of these elements (which may include another <set>). By default, all elements are applied simultaneously. To have them occur sequentially, you must specify the startOffset attribute.
<setandroid:shareInterpolator=boolean> // Only required if multiple tags are used. <alphaandroid:fromAlpha=float android:toAlpha=float> <scaleandroid:fromXScale=float android:toXScale=float android:fromYScale=float android:toYScale=float android:pivotX=string android:pivotY=string> <translateandroid:fromX=string android:toX=string android:fromY=string android:toY=string> <rotateandroid:fromDegrees=float android:toDegrees=float android:pivotX=string android:pivotY=string> <interpolatortag> <set> </set>
Elements and Attributes
<set>
A container that can recursively hold itself or other animations. Represents an AnimationSet. You can include as many child elements of the same or different types as you like. Supports the following attribute:
shareInterpolator - Whether to share the same Interpolator among all immediate child elements.
<alpha>
A fading animation. Represents an AlphaAnimation. Supports the following attributes:
fromAlpha - 0.0 to 1.0, where 0.0 is transparent.
toAlpha - 0.0 to 1.0, where 0.0 is transparent.
<scale>
A resizing animation. Represents a ScaleAnimation. You can specify what is the center point of the image (the pinned center), from which it grows outward (or inward), by specifying pivotX and pivotY. So, for example, if these were 0, 0 (top left corner), all growth would be down and to the right. scale supports the following attributes:
fromXScale - Starting X size, where 1.0 is no change.
toXScale - Ending X size, where 1.0 is no change.
fromYScale - Starting Y size, where 1.0 is no change.
toYScale - Ending Y size, where 1.0 is no change.
pivotX - The X coordinate of the pinned center.
pivotY - The Y coordinate of the pinned center.
<translate>
A vertical/horizontal motion animation. Represents a TranslateAnimation. Supports the following 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 with no suffix, indicating an absolute value.
fromXDelta - Starting X location.
toXDelta - Ending X location.
fromYDelta - Starting Y location.
toYDelta - Ending Y location.
<rotate>
A rotation animation. Represents a RotateAnimation. Supports the following attributes:
fromDegrees - Starting rotation, in degrees.
toDegrees - Ending rotation, in degrees.
pivotX - The X coordinate of the center of rotation, in pixels, where (0,0) is the top left corner.
pivotY - The Y coordinate of the center of rotation, in pixels, where (0,0) is the top left corner.
<interpolator tag>
You can also use any of the interpolator subclass elements defined in R.styleable. Examples include <CycleInterpolator>, <EaseInInterpolator>, and <EaseOutInterpolator>. These objects define a velocity curve that describes how quickly a visual action takes place on a timeline (fast at first and slow later, slow at first and gradually faster, and so on).
In addition to the attributes defined for each element above, the elements <alpha>, <scale>, <translate>, <rotate>, and <set> all support the following attributes (inherited from the Animation class):
You can optionally set an interpolator for each element to determine how quickly or slowly it performs its effect over time. For example, slow at the beginning and faster at the end for EaseInInterpolator, and the reverse for EaseOutInterpolator. A list of interpolators is given in R.anim. To specify these, use the syntax @android:anim/interpolatorName.
For more discussion and animation code samples, see the discussion in the 2D Graphics document.
Pixels - corresponds to actual pixels on the screen.
in
Inches - based on the physical size of the screen.
mm
Millimeters - based on the physical size of the screen.
pt
Points - 1/72 of an inch based on the physical size of the screen.
dp
Density-independent Pixels - an abstract unit that is based on the physical density of the screen. These units are relative to a 160 dpi screen, so one dp is one pixel on a 160 dpi screen. The ratio of dp-to-pixel will change with the screen density, but not necessarily in direct proportion. Note: The compiler accepts both "dip" and "dp", though "dp" is more consistent with "sp".
sp
Scale-independent Pixels - this is like the dp unit, but it is also scaled by the user's font size preference. It is recommend you use this unit when specifying font sizes, so they will be adjusted for both the screen density and user's preference.