Android Drawable series-Animation Drawable (Animation implementation)

Source: Internet
Author: User

Android Animation is implemented in Animation. In Android, there are two Animation modes:
Among them, Tween Animation is a gradient Animation by constantly performing image transformations (translation, scaling, and rotation) on the objects in the scenario, while Frame Animation: playing images prepared in advance in sequence is an animation of screen conversion.
The following is an example of an Android Animation configuration file. Relatively simple. However, the basic methods are useful as follows:
[Html]
<? Xml version = "1.0" encoding = "UTF-8"?>
<Set xmlns: android = "http://schemas.android.com/apk/res/android"
Android: interpolator = "@ android: anim/linear_interpolator"
>
<! -- Define scaling transformation -->
<Scale
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 = "true"
Android: duration= "2000"
/>

<! -- Define displacement change -->
<Translate
Android: fromXDelta = "10"
Android: toXDelta = "130"
Android: fromYDelta = "30"
Android: toYDelta = "-80"
Android: duration= "2000"
/>


</Set>
Four types can be defined in Set.
Alpha gradient transparency animation effect
Scale scaling Animation
Animated effect of moving the translate position
Rotate screen transfer rotation animation effect
Of course, Android certainly supports writing in Java code as long as it supports XML. These four classes must be the above Type plus Animation (AlphaAnimation, etc.), with the same effect. XML configuration of personal preference. Intuitive.
The following describes the implementation of the Animation Mode:
1. Tween Animation
A tween animation is a combination of different actions. If you have an EditText object, you can move it, rotate it, and make it larger or smaller. If there is a background image under the text, the background image will also be converted with the file.
Use XML to define Tween Animation
The XML file of the animation is in the res/anim directory of the project. The file must contain a root element, which can be described by the preceding four attribute values. Or put all the above elements in the <set> element group. By default, all animation commands occur at the same time. However, if you want them to occur in order, you must set startOffset. Animation commands define what kind of conversions you want to perform. when they happen, how long should they be executed? The conversions can be continuous or at the same time. For example, if you want to move the text content from the left to the right, then rotate the text 180 degrees, or rotate the text content simultaneously during the movement, no conversion requires some special parameters (the size of the start and end sizes, the rotation angle of the start and end, and so on). You can also set some basic parameters (for example, if several conversions occur simultaneously, you can set the same Start Time for them. If the conversion is based on the sequence, the calculation Start Time plus the cycle.

<Set android: Using interpolator = "false" xmlns: android = "http://schemas.android.com/apk/res/android">
<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>
How to Use Tween Animation
Use the static method loadAnimation () of the AnimationUtils class to load the animated XML file in XML.
// ImageView in main. xml
ImageView spaceshipImage = (ImageView) findViewById (R. id. spaceshipImage );
// Load the animation
Animation hyperspaceJumpAnimation = AnimationUtils. loadAnimation (this, R. anim. hyperspace_jump );
// Use ImageView to display animations
SpaceshipImage. startAnimation (hyperspaceJumpAnimation );
How to define animations in Java code
// Define the animation instance object in the code

Private Animation myAnimation_Alpha;

Private Animation myAnimation_Scale;

Private Animation myAnimation_Translate;

Private Animation myAnimation_Rotate;

// Initialize an instance object based on its own Constructor

MyAnimation_Alpha = new AlphaAnimation (0.1f, 1.0f );

MyAnimation_Scale = new ScaleAnimation (0.0f, 1.4f, 0.0f, 1.4f,

Animation. RELATIVE_TO_SELF, 0.5f, Animation. RELATIVE_TO_SELF, 0.5f );

MyAnimation_Translate = new TranslateAnimation (30366f,-80366f, 30366f, 300366f );

MyAnimation_Rotate = new RotateAnimation (0.0f, + 3500000f,

Animation. RELATIVE_TO_SELF, 0.5f, Animation. RELATIVE_TO_SELF, 0.5f );
Explanation of interpolator
Interpolator defines the rate of change of an animation ). This allows the basic animation effects (alpha, scale, translate, and rotate) to accelerate, slow down, and repeat.
Interpolator defines the speed at which an animation changes, including constant speed, positive acceleration, negative acceleration, and irregular acceleration. Interpolator is a base class that encapsulates all the common methods of Interpolator. It has only one method, that is, getInterpolation (float input ), this method maps a point on the timeline to a multiplier to be applied to the transformations of an animation. Android provides several Interpolator subclasses to achieve different speed curves, as shown below:

AccelerateDecelerateInterpolator The speed changes are slow at the beginning of the animation and in the middle of the introduction.
AccelerateInterpolator Speed changes are slow at the beginning of the animation and acceleration starts.
CycleInterpolator Specifies the number of times the animation is played cyclically, and the speed changes along the sine curve.
DecelerateInterpolator The speed changes slowly at the beginning of the animation, and then starts to slow down.
LinearInterpolator Changes at an even rate in an animation
 

Frame Animation
Frame Animation is an image prepared in advance for sequential playback, similar to a movie. Unlike the animation package, the Android SDK provides another class, AnimationDrawable, to define and use Frame Animation.
Frame Animation can be defined in XML Resource (or stored in the res \ anim folder), or by using the API definition in AnimationDrawable. Since Tween Animation and Frame Animation are very different, the XML definition format is also completely different. The format is: first, the root node of the animation-list, the animation-list root node contains multiple item subnodes. Each item node defines an animation, The drawable resource of the current frame, and the duration of the current frame. The following describes the elements of a node:
The following is a specific XML example to define a frame-by-frame animation:
<Animation-list xmlns: android = "http://schemas.android.com/apk/res/android"
Android: oneshot = "true">
<Item android: drawable = "@ drawable/rocket_thrust1" android: duration = "200"/>
<Item android: drawable = "@ drawable/rocket_thrust2" android: duration = "200"/>
<Item android: drawable = "@ drawable/rocket_thrust3" android: duration = "200"/>
</Animation-list>

Then we save the above XML in the res/anim/folder and name it rocket_thrust.xml to display the animation code:
AnimationDrawable rocketAnimation;

Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );

ImageView rocketImage = (ImageView) findViewById (R. id. rocket_image );
RocketImage. setBackgroundResource (R. anim. rocket_thrust );
RocketAnimation = (AnimationDrawable) rocketImage. getBackground ();
}

Public boolean onTouchEvent (MotionEvent event ){
If (event. getAction () = MotionEvent. ACTION_DOWN ){
RocketAnimation. start ();
Return true;
}
Return super. onTouchEvent (event );
}

 


From Jason's Java Column


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.