Android-animation effect

Source: Internet
Author: User

The Android SDK provides three types of animations: Compensation animation, frame-by-frame animation, and interpolation attribute animation. Next we will first introduce the first animation effect-the animation of the population.

The compensation animation can be applied to the View, so that developers can define changes to the size, position, rotation, and transparency to make the View content work.

The Animation is created using the Animation class. It has four direct sub-classes for different Animation effects:

AlphaAnimation gradient transparency animation effect, that is, fade-in and fade-out effect
ScaleAnimation: Scaling animation effect with gradient size, that is, Scaling Effect
TranslateAnimation
RotateAnimation: transfer the animation effect, that is, the animation effect.


There are two ways to use the effect of the animation to fill the room. The first is to set the animation effect in the XML file, and the second is to set it in the Java code. The following two methods are described respectively:

1. Set the method in the XML file:

Create an anim folder in the res directory of the Android project, and then create firstanim. xml in the anim folder to add the animation effect configuration code. The sample code is as follows:

[Html]
<? 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

FromAlpha indicates the transparency when the animation starts.
ToAlpha indicates the transparency when the animation ends.
Value description: 0.0 indicates completely transparent, 1.0 indicates completely opaque, and the above value is a float data type number between 0.0 and 1.0.
 
Duration is the animation duration in milliseconds.
-->

 
<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: duration = "700"/>
 
<! -- Scale up and down the animation effect
Interpolator specifies an animation insertor.
Several simple animation inserts:
Accelerate_decelerate_interpolator accelerates and then slows down.
Accelerate_interpolator acceleration, slow intermediate acceleration at the beginning
Decelerate_interpolator: Slow down at the beginning and then slow down
LinearInterpolator linear, linear and even change

FromXScale is the scaling size on the X coordinate when the animation starts.
ToXScale is the scaling size on the X coordinate when the animation ends.

FromYScale is the scaling size on the Y coordinate when the animation starts.
ToYScale is the scaling size on the Y coordinate when the animation ends.
The preceding four attribute values are described as follows:

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

TX is the starting position of the X coordinate of the animation relative to the object.
The starting position of the animation relative to the Y coordinate of the object.
The preceding two attribute values are from 0%-100%. 50% is the midpoint of the X or Y coordinate of the object.
 
Duration is the animation duration in milliseconds.
 
When fillAfter is set to true, the control is parked at the end of the animation.
-->
 
 
 
<Translate
Android: fromXDelta = "30"
Android: toXDelta = "-80"
Android: fromYDelta = "30"
Android: toYDelta = "300"
Android: duration= "2000"
/>
<! -- Translate position transfer animation effect

FromXDelta is the position on the X coordinate when the animation starts.
ToXDelta is the position on the X coordinate when the animation ends.
FromYDelta is the position on Y coordinate when the animation starts.
ToYDelta is the position on Y coordinate when the animation ends
If fromXType toXType fromYType toYType is not specified, it is a reference object of its own by default.
 
Duration animation duration, in milliseconds
-->
 
 
<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
Interpolator specifies an animation insertor.
FromDegrees indicates the starting point of the animation.
ToDegrees indicates that the Rotation Angle of an object can be greater than 360 degrees when the animation ends.
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)
 
TX is the starting position of the X coordinate of the animation relative to the object.
The starting position of the animation relative to the Y coordinate of the object.
The preceding two attribute values are from 0% to 100%.
Duration is the animation duration in milliseconds.
-->
</Set>

<? 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

FromAlpha indicates the transparency when the animation starts.
ToAlpha indicates the transparency when the animation ends.
Value description: 0.0 indicates completely transparent, 1.0 indicates completely opaque, and the above value is a float data type number between 0.0 and 1.0.

Duration is the animation duration in milliseconds.
-->
 

<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: duration = "700"/>

<! -- Scale up and down the animation effect
Interpolator specifies an animation insertor.
Several simple animation inserts:
Accelerate_decelerate_interpolator accelerates and then slows down.
Accelerate_interpolator acceleration, slow intermediate acceleration at the beginning
Decelerate_interpolator: Slow down at the beginning and then slow down
LinearInterpolator linear, linear and even change

FromXScale is the scaling size on the X coordinate when the animation starts.
ToXScale is the scaling size on the X coordinate when the animation ends.

FromYScale is the scaling size on the Y coordinate when the animation starts.
ToYScale is the scaling size on the Y coordinate when the animation ends.
The preceding four attribute values are described as follows:

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

TX is the starting position of the X coordinate of the animation relative to the object.
The starting position of the animation relative to the Y coordinate of the object.
The preceding two attribute values are from 0%-100%. 50% is the midpoint of the X or Y coordinate of the object.

Duration is the animation duration in milliseconds.

When fillAfter is set to true, the control is parked at the end of the animation.
-->

 

<Translate
Android: fromXDelta = "30"
Android: toXDelta = "-80"
Android: fromYDelta = "30"
Android: toYDelta = "300"
Android: duration= "2000"
/>
<! -- Translate position transfer animation effect

FromXDelta is the position on the X coordinate when the animation starts.
ToXDelta is the position on the X coordinate when the animation ends.
FromYDelta is the position on Y coordinate when the animation starts.
ToYDelta is the position on Y coordinate when the animation ends
If fromXType toXType fromYType toYType is not specified, it is a reference object of its own by default.

Duration animation duration, in milliseconds
-->


<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
Interpolator specifies an animation insertor.
FromDegrees indicates the starting point of the animation.
ToDegrees indicates that the Rotation Angle of an object can be greater than 360 degrees when the animation ends.
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)

TX is the starting position of the X coordinate of the animation relative to the object.
The starting position of the animation relative to the Y coordinate of the object.
The preceding two attribute values are from 0% to 100%.
Duration is the animation duration in milliseconds.
-->
</Set>

 

In the onCreate () method of the Activity, obtain the animation effect configured in XML. The Code is as follows:

[Java]
Animation animation = AnimationUtils. loadAnimation (this, R. anim. firstanim );

Animation animation = AnimationUtils. loadAnimation (this, R. anim. firstanim );
If the animation effect is used on an ImageView, you can refer to the following code:

[Java]
ImageView. startAnimation (animation );

ImageView. startAnimation (animation); 2. Set the method in Java code:

Take AlphaAnimation as an example,

[Java]
// First declare an object of Animation
Private Animation alpha;
 
 
// Instantiate this object in the onCreate () method of Activity
Alpha = new AlphaAnimation (0.1f, 1.0f );
 
 
 
// Set the animation duration to 3 seconds.
 
Alpha. setDuration (3000 );

// First declare an object of Animation
Private Animation alpha;


// Instantiate this object in the onCreate () method of Activity
Alpha = new AlphaAnimation (0.1f, 1.0f );

 

// Set the animation duration to 3 seconds.

Alpha. setDuration (3000 );

If the animation effect is used on an ImageView, you can refer to the following code:

[Java]
ImageView. startAnimation (alpha );

ImageView. startAnimation (alpha );

 

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.