Android-based animation effect Programming

Source: Internet
Author: User
Tags time in milliseconds

This article is from http://wangstar.javaeye.com/blog/409115. if the basic knowledge is correct, you can copy it to facilitate searching.

Animation effect programming basics-Android Animation

Animation type

Android animation consists of four types
In XML
Alpha
Gradient transparency animation effect
Scale
Gradient scaling animation effect
Translate
Animation Effect
Rotate
Image Rotation animation effect

Java code
AlphaAnimation
Gradient transparency animation effect
ScaleAnimation
Gradient scaling animation effect
TranslateAnimation
Animation Effect
RotateAnimation
Image Rotation animation effect

Android Animation Mode
Animation mainly has two Animation modes:

One is tweened animation (gradient animation)
In XML
JavaCode
Alpha
AlphaAnimation
Scale
ScaleAnimation

One is frame by frame (screen conversion animation)
In XML
JavaCode
Translate
TranslateAnimation
Rotate
RotateAnimation

How to define an animation in an XML file

① Open Eclipse and create an Android Project
② Create an anim folder in the res directory
③ Create a new myanim. xml file in the anim directory (note that the file name is in lower case)
④ Add XML animation code
<? Xml version = "1.0" encoding = "UTF-8"?>
<Set xmlns: android = "http://schemas.android.com/apk/res/android">
<Alpha/>
<Scale/>
<Translate/>
<Rotate/>
</Set>
Copy code

Android animation parsing-XML

<Alpha>
<? Xml version = "1.0" encoding = "UTF-8"?>
<Set xmlns: android = "http://schemas.android.com/apk/res/android">
<Alpha
Android: fromalphi = "0.1"
Android: toAlpha = "1.0"
Android: duration = "3000"/>
<! -- Transparency controls the animation effect alpha
Floating point value:
The fromAlpha attribute is the transparency when the animation starts.
The toAlpha attribute is the transparency when the animation ends.
Note:
0.0 indicates completely transparent
1.0 indicates completely opaque
The above value is a float data type number between 0.0 and 1.0.

Long integer value:
The duration attribute is the animation duration.
Note:
Time in milliseconds
-->
</Set>
Copy code

<Scale>
<? Xml version = "1.0" encoding = "UTF-8"?>
<Set xmlns: android = "http://schemas.android.com/apk/res/android">
<Scale
Android: interpolator =
"@ Android: anim/accelerate_decelerate_interpolator"
Android: fromXScale = "0.0"
Android: toXScale = "1.4"
Android: fromYScale = "0.0"
Android: toYScale = "1.4"
Android: Required Tx = "50%"
Android: Ty = "50%"
Android: fillAfter = "false"
Android: startOffset = "700"
Android: duration = "700"/>
</Set>
<! -- Scale up and down the animation effect
Attribute: interpolator specifies an animation plug-in.
When using resources in android. res. anim
There are three types of animation inserts:
Accelerate_decelerate_interpolator acceleration-Deceleration animation inserter
Accelerate_interpolator acceleration-animation insertor
Decelerate_interpolator deceleration-animation insertor
Other animation Effects
Floating point value:

The fromXScale attribute is the scaling size on the X coordinate when the animation starts.
The toXScale attribute is the scaling size on the X coordinate when the animation ends.

The fromYScale attribute is the scaling size on the Y coordinate at the start of the animation.
The toYScale attribute is the scaling size on the Y coordinate when the animation ends.
The startOffset attribute indicates the time from when the animation was stopped.

Note:
The preceding four property values

0.0 indicates shrinking to none
1.0 indicates normal scaling-free
A value smaller than 1.0 indicates contraction.
Value greater than 1.0 indicates Amplification

The ready TX attribute is the starting position of the animation relative to the X coordinate of the object.
The ready ty attribute is the starting position of the animation relative to the Y coordinate of the object.

Note:
The preceding two attribute values are from 0% to 100%.
50% indicates the point position on the X or Y coordinate of the object.

Long integer value:
The duration attribute is the animation duration.
Description: The time is in milliseconds.

Boolean value:
When the fillAfter attribute is set to true, the animation is applied after the animation ends.
-->
Copy code


<Translate>

<? Xml version = "1.0" encoding = "UTF-8"?>
<Set xmlns: android = "http://schemas.android.com/apk/res/android">
<Translate
Android: fromXDelta = "30"
Android: toXDelta = "-80"
Android: fromYDelta = "30"
Android: toYDelta = "300"
Android: duration= "2000"
/>
<! -- Translate position transfer animation effect
Integer value:
The fromXDelta attribute is the position on the X coordinate when the animation starts.
The toXDelta attribute is the position on the X coordinate when the animation ends.
The fromYDelta attribute is the position on the Y coordinate when the animation starts.
The toYDelta attribute is the position on the Y coordinate when the animation ends.
Note:
When fromXType toXType fromYType toYType is not specified,
The default object is the reference object of the user.
Long integer value:
The duration attribute is the animation duration.
Description: The time is in milliseconds.
-->
</Set>
Copy code

<Rotate>

<? Xml version = "1.0" encoding = "UTF-8"?>
<Set xmlns: android = "http://schemas.android.com/apk/res/android">
<Rotate
Android: interpolator = "@ android: anim/accelerate_decelerate_interpolator"
Android: fromDegrees = "0"
Android: toDegrees = "+ 350"
Android: Required Tx = "50%"
Android: Ty = "50%"
Android: duration = "3000"/>
<! -- Rotate rotation animation effect
Attribute: interpolator specifies an animation plug-in.
When using resources in android. res. anim
There are three types of animation inserts:
Accelerate_decelerate_interpolator acceleration-Deceleration animation inserter
Accelerate_interpolator acceleration-animation insertor
Decelerate_interpolator deceleration-animation insertor
Other animation Effects

Floating point value:
The fromDegrees attribute is the object angle when the animation starts.
The toDegrees attribute indicates that the Rotation Angle of an object can be greater than 360 degrees when the animation ends.

Note:
If the angle is negative, it indicates clockwise rotation.
If the angle is positive, it indicates clockwise rotation.
(Negative from -- to positive: clockwise rotation)
(Negative from -- to negative: counter-clockwise rotation)
(Positive from -- to positive: clockwise rotation)
(Positive from -- to negative: clockwise rotation)

The ready TX attribute is the starting position of the animation relative to the X coordinate of the object.
The ready ty attribute is the starting position of the animation relative to the Y coordinate of the object.

Note: The preceding two attribute values are from 0% to 100%.
50% indicates the point position on the X or Y coordinate of the object.

Long integer value:
The duration attribute is the animation duration.
Description: The time is in milliseconds.
-->
</Set>
Copy code


How to Use the animation effect in XML
Public static Animation loadAnimation (Context context, int id)
// The first parameter Context is the Context of the program
// The second parameter id is the reference of the animated XML file.
// Example:
MyAnimation = AnimationUtils. loadAnimation (this, R. anim. my_action );
// Use the static method loadAnimation () of the AnimationUtils class to load the animation XML file in XML
Copy code

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 );
Copy code


Android animation parsing-JavaCode

AlphaAnimation

① AlphaAnimation class object definition
Private AlphaAnimation myAnimation_Alpha;
Copy code
② AlphaAnimation Class Object Construction
AlphaAnimation (float fromAlpha, float toAlpha)
// The first parameter fromAlpha indicates the transparency when the animation starts.
// The second toAlpha parameter indicates the transparency at the animation end.
MyAnimation_Alpha = new AlphaAnimation (0.1f, 1.0f );
// Description:
// 0.0 indicates completely transparent
// 1.0 indicates completely opaque
Copy code
③ Set the animation duration
MyAnimation_Alpha.setDuration (5000 );
// Set the duration to 5000 milliseconds
Copy code



ScaleAnimation

① ScaleAnimation class object definition
Private AlphaAnimation myAnimation_Alpha;
Copy code
② ScaleAnimation Class Object Construction
ScaleAnimation (float fromX, float toX, float fromY, float toY,
Int struct txtype, float struct txvalue, int struct tytype, float struct tyvalue)
// The first parameter fromX is the scaling size on the X coordinate when the animation starts.
// The second toX parameter is the scaling size on the X coordinate at the animation end.
// The third parameter fromY is the scaling size on the Y coordinate when the animation starts.
// The fourth toY parameter is the scaling size on the Y coordinate at the animation end.
/* Description:
The preceding four property values
0.0 indicates shrinking to none
1.0 indicates normal scaling-free
A value smaller than 1.0 indicates contraction.
Value greater than 1.0 indicates Amplification
*/
// The Fifth parameter "animation txtype" indicates the animation position type on the X axis relative to the object.
// The sixth parameter, txvalue, is the starting position of the animation relative to the X coordinate of the object.
// The Seventh parameter "animation txtype" is the animation position type on the Y axis relative to the object.
// The eighth parameter polictyvalue is the starting position of the animation relative to the Y coordinate of the object.
MyAnimation_Scale = new ScaleAnimation (0.0f, 1.4f, 0.0f, 1.4f,
Animation. RELATIVE_TO_SELF, 0.5f, Animation. RELATIVE_TO_SELF, 0.5f );
Copy code
③ Set the animation duration
MyAnimation_Scale.setDuration (700 );
// Set the duration to 700 milliseconds
Copy code

TranslateAnimation


① TranslateAnimation class object definition
Private AlphaAnimation myAnimation_Alpha;
Copy code
② Construct a TranslateAnimation Class Object
TranslateAnimation (float fromXDelta, float toXDelta,
Float fromYDelta, float toYDelta)
// The first parameter fromXDelta is the moving position on the X coordinate when the animation starts.
// The second parameter toXDelta is the moving position on the X coordinate when the animation ends.
// The third parameter fromYDelta is the moving position on the Y coordinate when the animation starts.
// The fourth parameter toYDelta is the moving position of the Y coordinate at the animation end.
Copy code
③ Set the animation duration
MyAnimation_Translate.setDuration (2000 );
// Set the duration to 2000 milliseconds
Copy code


RotateAnimation
① RotateAnimation class object definition
Private AlphaAnimation myAnimation_Alpha;
Copy code

② RotateAnimation Class Object Construction
RotateAnimation (float fromDegrees, float toDegrees,
Int struct txtype, float struct txvalue, int struct tytype, float struct tyvalue)
// The first parameter fromDegrees is the rotation angle at the start of the animation.
// The second parameter toDegrees indicates the animation rotation angle.
// The third parameter "animation txtype" indicates the animation position type on the X axis relative to the object.
// The fourth argument txvalue is the starting position of the animation relative to the X coordinate of the object.
// The Fifth parameter "effectxtype" indicates the animation position type on the Y axis relative to the object.
// The sixth parameter polictyvalue is the starting position of the animation relative to the Y coordinate of the object.
MyAnimation_Rotate = new RotateAnimation (0.0f, + 3500000f,
Animation. RELATIVE_TO_SELF, 0.5f, Animation. RELATIVE_TO_SELF, 0.5f );
Copy code

③ Set the animation duration
MyAnimation_Rotate.setDuration (3000 );
// Set the duration to 3000 milliseconds
Copy code


How to Use the animation effect in Java code

Use the startAnimation () method inherited from the View parent class to add an animation effect for the View or subclass View.

Related Article

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.