You can use the View animation system to execute the animation on the View object. A complementary animation is an animation that is calculated using starting point, ending point, size, rotation, and other animation features.
The compensation animation can execute a simple conversion series (Position, size, rotation, and transparency) on the content of the View object ). Therefore, if you have a TextView object, you can move, rotate, zoom in, or zoom out the text. If the TextView object has a background image, the background image will be changed along with the text. The animation package provides all the classes used in the compensation animation.
The animation instruction sequence defines the supplementary animation. These commands can be defined either in XML or in Android code. Like layout definition, we recommend that you use XML to define an animation because it is more readable, reusable, and pluggable than an animation to be encoded. In the following example, we use XML. (To learn more about defining animations in application code, read the AnimationSet class and other Animation subclasses .)
Animation commands define the animation transformations you want, as well as the timing of the animation and the playback duration of the animation. Animation transformations can be sequential or concurrent. For example, there is a TextView object content that is moved from left to right, then rotated 180 degrees, or simultaneously rotated when the text is moved. Each transformation requires a set of parameters to specify the desired transformation (for the starting and ending dimensions of the dimensional transformation, for the starting and ending dimensions of the rotation, and for the ending angle ), and a group of common parameters (for example, start time and duration ). If several transformations occur at the same time, you need to set the same start time for them; if you want them to play in sequence, the Start Time and the length of the previous animation transformation are used to calculate the start time of the next animation.
The animation XML file must be defined in the res/anim/directory of your Android project. This file must have a separate root element: This element can be a separate <alpha>, <scal>, <translate>, <rotate> interpolation element, it can also be a <set> element that combines these elements (including <set> elements. By default, all animation commands are concurrent. To make them happen in sequence, you must specify the startOffset attribute as shown in the following example.
The following XML is from a View object in APIDemo that is used for stretching and rotating at the same time:
<Set android: Using interpolator = "false">
<Scale
Android: interpolator = "@ android: anim/accelerate_decelerate_interpolator"
Android: fromXScale = "1.0"
Android: toXScale = "1.4"
Android: fromYScale = "1.0"
Android: toYScale = "0.6"
Android: Required Tx = "50%"
Android: Ty = "50%"
Android: fillAfter = "false"
Android: duration = "700"/>
<Set android: interpolator = "@ android: anim/decelerate_interpolator">
<Scale
Android: fromXScale = "1.4"
Android: toXScale = "0.0"
Android: fromYScale = "0.6"
Android: toYScale = "0.0"
Android: Required Tx = "50%"
Android: Ty = "50%"
Android: startOffset = "700"
Android: duration= "400"
Android: fillBefore = "false"/>
<Rotate
Android: fromDegrees = "0"
Android: toDegrees = "-45"
Android: toYScale = "0.0"
Android: Required Tx = "50%"
Android: Ty = "50%"
Android: startOffset = "700"
Android: duration = "400"/>
</Set>
</Set>
The coordinates of the screen in the upper left corner (not used in the preceding example) are (0, 0) and gradually increase to the lower right corner.
Some values, such as ipvtx, can be specified relative to the object itself or its parent container. The expected value must be in the correct format (50 indicates 50% relative to the upper left corner of its parent container, and 50% indicates 50% relative to its upper left corner ).
By assigning an Interpolator object, you can decide how to transform an animation over time. Android includes several Interpolator subclasses that can specify the curves of various speeds. For example, AccelerateInterpolator tells the system to start slowly and then gradually accelerate the transformation. Each transformation has a property value applied to XML.
Hyperspace_jump.xml file saved in the res/anim/directory of the project. The following code references this file and applies it to an ImageView object from the layout.
ImageView spaceshipImage = (ImageView) findViewById (R. id. spaceshipImage );
Animation hyperspaceJumpAnimation = AnimationUtils. loadAnimation (this, R. anim. hyperspace_jump );
SpaceshipImage. startAnimation (hyperspaceJumpAnimation );
As an alternative to the startAnimation () method, you can use Animation. setStartTime () method to define the animation start time, and then use View. the setAnimation () method assigns the animation object to the View object.
Note: No matter how your animation moves or adjusts the size, the boundaries of View objects with the animation will not be automatically adjusted to adapt to changes, even if the animation exceeds the View object's boundary, it will not be cropped, but if the animation exceeds its parent container's boundary, it will be cropped.
By FireOfStar