Android anim animation effect programming Basics

Source: Internet
Author: User
Tags time in milliseconds

Animation effect programming basics-animationandroid

Animation type

Android animation consists of four types

In XML
Alpha
Gradient transparency animation effect
Scale
Gradient scaling animation effect
Translate
Animation Effect
Rotate
Animation effects of screen transfer and Rotation


Java code
Alphaanimation
Gradient transparency animation effect
Scaleanimation
Gradient scaling animation effect
Translateanimation
Animation Effect
Rotateanimation
Animation effects of screen transfer and Rotation

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, 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.

Animation effect order

Android: animationorder = "random" // random

Android: animationorder = "reverse" // reverse

Gridlayoutanimation

Android: direpripriority = "row"

Android: direpripriority = "column"

Android: Direction = "right_to_left | bottom_to_top"

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/lzx_bupt/archive/2010/04/19/5502223.aspx

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.