The Android platform offers two types of animations. One is the tween animation, that is, the objects in the scene constantly changing the image to produce animation effects (rotation, translation, scaling and gradient).
Let's talk about Tweene animations.
Main class:
Animation Animation
Alphaanimation Gradient Transparency
Rotateanimation Picture Rotation
Scaleanimation Gradient Dimension Scaling
Translateanimation position Move
Animationset of the anthology
I. alphaanimation
The first parameter of the Alphaanimation class Fromalpha the transparency at the beginning of the animation, and the second argument toalpha the transparency at the end of the animation.
Setduration is used to set the duration of the animation.
two. Rotateanimation
The first parameter of the Rotateanimation class fromdegrees the angle at which the animation starts, and the second argument todegrees the angle at the end of the animation.
In addition, you can set the telescopic mode Pivotxtype, Pivotytype, telescopic animation relative to the x,y coordinates of the start position pivotxvalue, Pivotyvalue, and so on.
three. Scaleanimation
In the Scaleanimation class
The first argument is FROMX, and the second parameter Tox: The expansion dimension at the beginning and end of the animation, respectively.
The third parameter is fromy, and the fourth parameter toy: The scaling dimension at the beginning and end of the animation, at the y-coordinate.
In addition, you can set the telescopic mode Pivotxtype, Pivotytype, telescopic animation relative to the x,y coordinates of the start position pivotxvalue, Pivotyvalue, and so on.
four. Translateanimation
The first argument is Fromxdelta, and the second parameter Toxdelta: The start of the animation, and the X coordinate at the end.
The third parameter is Fromydelta, and the fourth parameter Toydelta: The start of the animation, and the y-coordinate at the end.
The following example I implemented is to make the picture have the above four animation effect, and five achieve is two effect overlap, concrete implementation
screenshot below:
Clicking on each button will respond accordingly.
The layout files used in this example are as follows:
Copy Code code as follows:
<?xml version= "1.0" encoding= "Utf-8"?>
<absolutelayout
Android:layout_width= "Fill_parent"
android:layout_height= "Fill_parent"
Xmlns:android= "Http://schemas.android.com/apk/res/android" >
<button
Android:id= "@+id/button_scale"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:text= "Scale"
android:layout_x= "5DP"
android:layout_y= "383DP"/>
<button
Android:id= "@+id/button_rotate"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:text= "Rotate"
android:layout_x= "158DP"
android:layout_y= "383DP"/>
<button
Android:id= "@+id/button_alpha"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:text= "Alpha"
android:layout_x= "5DP"
android:layout_y= "331DP"/>
<button
Android:id= "@+id/button_translate"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:text= "Translate"
android:layout_x= "160DP"
android:layout_y= "329DP"/>
<button
Android:id= "@+id/button_alpha_translate"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:text= "Alpha_translate"
android:layout_x= "84DP"
android:layout_y= "265DP"/>
<imageview
Android:id= "@+id/imageview"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:layout_x= "105DP"
Android:layout_y= "133DP"
android:src= "@drawable/ic_launcher"
/>
</AbsoluteLayout>
The source code to implement this example is as follows:
Copy Code code as follows:
public class Animations_activity extends activity {
Private Button button1;
Private Button button2;
Private Button Button3;
Private Button Button4;
Private Button button5;
Private ImageView ImageView;
@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_animations_);
button1= (Button) Findviewbyid (R.id.button_alpha);
Button2= (Button) Findviewbyid (r.id.button_rotate);
button3= (Button) Findviewbyid (R.id.button_scale);
button4= (Button) Findviewbyid (r.id.button_translate);
button5= (Button) Findviewbyid (r.id.button_alpha_translate);
imageview= (ImageView) Findviewbyid (R.id.imageview);
Button1.setonclicklistener (New MyButton ());
Button2.setonclicklistener (New MyButton ());
Button3.setonclicklistener (New MyButton ());
Button4.setonclicklistener (New MyButton ());
Button5.setonclicklistener (New MyButton ());
}
Class MyButton implements onclicklistener{
@Override
public void OnClick (View arg0) {
TODO auto-generated Method Stub
Switch (Arg0.getid ()) {
Case R.id.button_alpha:
Alpha ();
Break
Case R.id.button_rotate:
Rotata ();
Break
Case R.id.button_scale:
Scale ();
Break
Case R.id.button_translate:
Translate ();
Break
Case R.id.button_alpha_translate:
Alpha_translate ();
Break
Default
Break
}
}
}
/*
* 1. Create a Animationset object that stores a collection of animations
* 2. Create corresponding animation objects as needed
* 3. According to the needs of the animation, set the appropriate data for the animation object (that is, the execution effect)
* 4 Award Animation object added to Animationset object
* 5. Start execution with control object Animationset
*/
public void Alpha () {
Animationset animationset=new Animationset (true);
Alphaanimation alphaanimation=new alphaanimation (1, 0);
Alphaanimation.setduration (2000);
Animationset.addanimation (alphaanimation);
Imageview.startanimation (Animationset);
}
public void Rotata () {
Animationset animationset=new Animationset (true);
The following four parameters define the position of the center of rotation
Rotateanimation rotateanimation=new Rotateanimation (
0, 360,
Animation.relative_to_parent, 1f,
Animation.relative_to_parent, 0f);
Rotateanimation.setduration (2000);
Animationset.addanimation (rotateanimation);
Imageview.startanimation (Animationset);
}
public void Scale () {
Animationset animationset=new Animationset (true);
Scaleanimation scaleanimation=new Scaleanimation (
1, 0.1f, 1, 0.1f,
Animation.relative_to_self, 0.5f,
Animation.relative_to_self, 0.5f);
Scaleanimation.setduration (2000);
Animationset.addanimation (scaleanimation);
Imageview.startanimation (scaleanimation);
}
public void Translate () {
Animationset animationset=new Animationset (true);
Translateanimation translateanimation=new Translateanimation (
Animation.relative_to_self, 0f, starting position of the//x axis
Animation.relative_to_self, 0.5f, end position of//x axis
Animation.relative_to_self, 0f, starting position of the//y axis
Animation.relative_to_self, 1.0f); End position of Y axis
Translateanimation.setduration (2000);
Animationset.addanimation (translateanimation);
/*
* If the first row is true, the effect is fixed after execution after the animation is finished
* If the second row of the setting is false, the effect is fixed after the animation has finished executing state
* The third line sets a long value, which is the number of milliseconds after which the animation is delayed.
* Line fourth defines an animation that repeats several times
*/
Animationset.setfillafter (TRUE);
Animationset.setfillbefore (FALSE);
Animationset.setstartoffset (2000);
Animationset.setrepeatcount (3);
Imageview.startanimation (Animationset);
}
public void Alpha_translate () {
Animationset animationset=new Animationset (true);
Alphaanimation alphaanimation=new alphaanimation (1, 0);
Alphaanimation.setduration (2000);
Animationset.addanimation (alphaanimation);
Translateanimation translateanimation=new Translateanimation (
Animation.relative_to_self, 0f, starting position of the//x axis
Animation.relative_to_self, 0.5f, end position of//x axis
Animation.relative_to_self, 0f, starting position of the//y axis
Animation.relative_to_self, 1.0f); End position of Y axis
Translateanimation.setduration (2000);
Animationset.addanimation (translateanimation);
Imageview.startanimation (Animationset);
}
@Override
public boolean Oncreateoptionsmenu (Menu menu) {
Inflate the menu; This adds items to the action bar if it is present.
Getmenuinflater (). Inflate (R.menu.activity_animations_, menu);
return true;
}
}