Android JDK provides us with 4 animation effects, namely: Alphaanimation,rotateanimation, Scaleanimation, Translateanimation. Today I would like to explain the animation effect of translateanimation. I was also in the animation of a mobile image of the project, encountered some problems. On the internet to check a lot of information, for several days. Finally understand how to use this translateanimation, recorded in this article, so that later forgotten can be consulted.
http://gundumw100.iteye.com/category/156622
9.4 Scaleanimation class: Dimensional change Animation class
The Scaleanimation class is an animated class of dimensional changes in an Android system that controls the size change of a View object that inherits from the animation class. Many of the methods in the Scaleanimation class are consistent with the animation class, and the most common method in this class is the Scaleanimation construction method.
"Basic Syntax" public scaleanimation (float FromX, float toX, float fromY, float toY, int pivotxtype, float pivotxvalue, int pivoty Type, float Pivotyvalue)
Parameter description
FromX: The scaling dimension on the starting X coordinate.
ToX: Ends the Scaling dimension on the x-coordinate.
FromY: The scaling dimension on the starting y-coordinate.
ToY: Ends the scaling dimensions on the y-coordinate.
Pivotxtype:x axis of the scaling mode, you can take the value of absolute, relative_to_self, relative_to_parent.
The scaling value of the pivotxvalue:x coordinates.
Pivotytype:y axis of the scaling mode, you can take the value of absolute, relative_to_self, relative_to_parent.
The scaling value of the pivotyvalue:y coordinates.
The example below demonstrates how to set a simple dimensional change animation effect by using code.
- public class Firstactivity extends Activity {
- /** called when the activity is first created. */
- @Override
- public void OnCreate (Bundle savedinstancestate) {//overloaded OnCreate method
- Super.oncreate (savedinstancestate);
- Setcontentview (R.layout.main);
- Final ImageView image= (ImageView) Findviewbyid (r.id.imageview1);//imageview Object
- Button btn1= (button) Findviewbyid (R.id.button1); Button Object
- Button btn2= (button) Findviewbyid (R.id.button2);
- Final Animation scaleanimation= new
- Scaleanimation (0f,1f,0f,1f,animation.relative_to_self,0.5f,animation.relative_to_self,0.5f);
- Set the size Change animation object
- Btn1.setonclicklistener (New View.onclicklistener () {//Set listener
- @Override
- public void OnClick (View v) {
- TODO auto-generated Method Stub
- Scaleanimation.setduration (2000); Animation duration
- Image.setanimation (scaleanimation); Set animation
- Scaleanimation.startnow (); Start animation
- }
- });
- Btn2.setonclicklistener (New View.onclicklistener () {//Set listener
- @Override
- public void OnClick (View v) {
- TODO auto-generated Method Stub
- Scaleanimation.cancel (); Cancel Animation execution
- }
- });
- }
- }
In this code, you first create an animated object with a dimension change by Scaleanimation construction method. Then, the duration of the animation is set in the first button listener, and then the animation is started. Cancels the animation in a second button listener. The reader runs this code and will see the picture gradually changing from small to large, as shown in 9.9. Finally, the picture is increased to the original size when it stops, as shown in 9.10.
|
| Figure 9.9 Dimensional Change animation |
|
| Figure 9.10 Original Size picture |
The translateanimation is a moving animation effect. It has three constructors, namely:
1.public translateanimation (Context context,attributeset attrs) Skip
2.public translateanimation (float fromxdelta, float toxdelta, float fromydelta, float toydelta)
This is one of our most common construction methods,
Float Fromxdelta: This parameter represents the difference between the point at which the animation starts and the current view x coordinate;
Float Toxdelta, which represents the difference between the point at which the animation ends and the current view x coordinate;
Float Fromydelta, which represents the difference between the starting point of the animation and the current view y-coordinate;
float Toydelta) This parameter represents the difference between the point at which the animation starts and the current view y-coordinate;
If the view is in a (x, y) point then the animation is moved from point B (X+fromxdelta, Y+fromydelta) to point C (X+toxdelta,y+toydelta).
3.Public translateanimation (int fromxtype, float fromxvalue, int toxtype, float toxvalue, int fromytype, Flo At Fromyvalue, int toytype, float toyvalue)
Fromxtype: The first parameter is a reference to the value in the x-axis direction (Animation.absolute, Animation.relative_to_self,or animation.relative_to_parent);
Fromxvalue: The second parameter is the starting value of the first parameter type;
Toxtype,toxvalue: The third parameter and the fourth parameter are the end references and corresponding values in the x-axis direction;
The following four parameters are not explained. If you select Animation.absolute all, it is actually the second constructor.
Take the x-axis as an example to describe the relationship between references and corresponding values:
If the selection reference is Animation.absolute, then the corresponding value should be a specific coordinate value, such as 100 to 300, which refers to absolute screen pixel units
If the selection reference is animation.relative_to_self or animation.relative_to_parent refers to a relative to itself or to a parent control, the corresponding value should be interpreted as relative to itself or the number of times or percent of the parent control. The multi-test parameter is clear.
Android Scaleanimation class: Dimensional change Animation class