Tween animation, also known as "compensatory Animation" and "Intermediate Animation", does not matter. It seems that many people know Lu Xun but do not know his name as Zhou Shuren.
Tween animations are divided into four types in Android: alphaanimation, translateanimation, scaleanimation, and rotateanimation ). All are inherited from Android. view. animation class, which is a process of changing from State A to State B. Therefore, the English name is tween animation, the Chinese name is complement animation, and the intermediate animation ". In general, there are two implementation methods: Java code (Java source code) and XML (xml configuration file ).
Code start
In the past, it was because there were a lot of constructors in each tween animation that were unclear. Now I have read them carefully and recorded them for later viewing.
Alphaanimation (Transparency animation)
Alphaanimation has two constructors:
-- Alphaanimation (context, attributeset attrs): The second parameter is an attribute set, which will be explained in detail later.
-- Alphaanimation (float fromalpha, float toalpha): the first parameter is the initial transparency, and the second parameter is the termination transparency.
Translateanimation (Pan animation)
Translateanimation has three constructors:
-- Translateanimation (context, attributeset attrs): skipped
-- Translateanimation (float fromxdelta, float toxdelta, float fromydelta, float toydelta): corresponds to the start and end coordinates of the X axis and the start and end coordinates of the Y axis respectively.
-- Translateanimation (INT fromxtype, float fromxvalue, int toxtype, float toxvalue, int fromytype, float fromyvalue, int toytype, float toyvalue ): the first parameter is the reference (animation. absolute, animation. relative_to_self,
Or animation. relative_to_parent); the second parameter is the starting value of the first parameter type; the third parameter and the fourth parameter are in the X axis direction.
The end point reference and the corresponding value. The last four parameters do not need to be explained. If you select animation. Absolute, it is actually the second constructor.
Take the X axis as an example to introduce the relationship between references and corresponding values.
If you select "animation. Absolute" as the reference, the corresponding value should be the specific coordinate value, for example, 100 to 300, which indicates the absolute screen pixel unit.
If you select either animation. relative_to_self or animation. relative_to_parent,
The corresponding value should be interpreted as several times or percent relative to itself or the parent control. Please try these parameter types more!
Scaleanimation (scaling animation)
Scaleanimation has four constructor functions:
-- Scaleanimation (context, attributeset attrs): skipped
-- Scaleanimation (float fromx, float tox, float fromy, float toy): Same as translateanimation (float fromxdelta, float toxdelta, float fromydelta, float toydelta)
-- Scaleanimation (float fromx, float tox, float fromy, float toy, float character Tx, float character ty): here we will explain the following two parameters:
Pivot, that is, pivot. These two parameters can be used to control the zoom direction of the zoom animation. This point does not change with the object size.
-- Scaleanimation (float fromx, float tox, float fromy, float toy, int limit txtype, float limit txvalue, int limit tytype, float limit tyvalue): If you understand what we have mentioned earlier, this is not a long description. If you are not clear about it, try using more code.
Rotateanimation (rotation animation)
Rotateanimation also has four constructor functions:
-- Rotateanimation (context, attributeset attrs)
-- Rotateanimation (float fromdegrees, float todegrees)
-- Rotateanimation (float fromdegrees, float todegrees, float limit Tx, float limit ty)
-- Rotateanimation (float fromdegrees, float todegrees, int limit txtype, float limit txvalue, int limit tytype, float limit tyvalue)
No nonsense here!
After talking about this, go directly to the code.
Here is the Java source code
Package com. tfsp. Training. testtweenanimation;
Import Android. App. activity;
Import Android. OS. Bundle;
Import Android. View. view;
Import Android. View. View. onclicklistener;
Import Android. View. animation. alphaanimation;
Import Android. View. animation. animation;
Import Android. View. animation. rotateanimation;
Import Android. View. animation. scaleanimation;
Import Android. View. animation. translateanimation;
Import Android. widget. arrayadapter;
Import Android. widget. Button;
Import Android. widget. imageview;
Import Android. widget. spinner;
Public class testtweenanimation extends activity {
// Define the start button
Private button start = NULL;
// Define the animation type drop-down list
Private spinner select = NULL;
// This picture is the animation performer
Private imageview IMG = NULL;
// Define the animation
Private animation tanimation = NULL;
// Define a string array to construct the adapter for the drop-down list
Private string STR [] = {
"Translation Animation", "Transparency Animation", "rotation Animation", "Scaling Animation"
};
@ Override
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. Main );
// Obtain each control from the XML file.
Start = (button) findviewbyid (R. Id. startbutton );
Select = (spinner) findviewbyid (R. Id. Select );
IMG = (imageview) findviewbyid (R. Id. IMG );
// Instantiate the adapter
Arrayadapter <string> adapter = new arrayadapter <string> (this, Android. R. layout. simple_spinner_item, STR );
Select. setadapter (adapter );
// Set the listener for the start button
Start. setonclicklistener (New onclicklistener (){
@ Override
Public void onclick (view v ){
Initialanimation ();
IMG. startanimation (tanimation );
}
});
}
// Initialize the animation
Public void initialanimation (){
Switch (select. getselecteditemposition ()){
Case 0:
Tanimation = new translateanimation (0,300, 50, 50 );
// Tanimation = new translateanimation (animation. relative_to_self, 0.0f, animation. accuracy,-0.5f, animation. accuracy,-0.5f );
Break;
Case 1: tanimation = new alphaanimation (0.1f, 1.0f );
Break;
Case 2: tanimation = new rotateanimation (0.0f, + 360.0f );
Break;
Case 3:
// Tanimation = new scaleanimation (0.0f, 1.0f, 0.0f, 1.0f );
Tanimation = new scaleanimation (0.0f, 1.0f, 0.0f, 1.0f, 200366f, 0.0f );
Break;
}
// Time required for animation completion
Tanimation. setduration (2000 );
}
}
Here is main. xml
<? XML version = "1.0" encoding = "UTF-8"?>
<Relativelayout xmlns: Android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
>
<Spinner
Android: Id = "@ + ID/select"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
/>
<Button
Android: Id = "@ + ID/startbutton"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: layout_below = "@ ID/select"
Android: text = "start playing"
/>
<Imageview
Android: Id = "@ + ID/IMG"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: layout_alignparentbottom = "true"
Android: src = "@ drawable/Sun"
/>
</Relativelayout>
Source code: http://www.rayfile.com/zh-cn/files/62056542-5b72-11e0-9308-0015c55db73d/
Go to: http://hi.baidu.com/soodroid/blog/item/8f7e661e8d69f4144b90a7e8.html