Two days ago received a task to do a UI, useful to the animation, so the time to see the next Android animation related knowledge.
There are four types of Android animation, namely
Alpha Transparency Animation
Scale size Scaling Animation
Translate Displacement Animation
Rotate Rotate Animation
These four types of animations can be categorized by pattern:
tweened Animation (gradient animation)--alpha and scale
Frame by frame (picture conversion animation)--translate and rotate
Tell me about the various properties of the rotate animation I've learned:
In the XML:
The official rotate attribute is given as shown above.
Android:drawable pictures that need to be animated for rotation
The starting point of the android:fromdegrees rotation (the angle at which the rotation begins)
Android:todegrees end point of rotation (rotation final angle)
Andoird:pivotx x value of the rotation point (offset from the left)
Android:pivoty y value of the rotation point (offset from the top)
Android:visible this good understanding, is the initial display state of the picture
My understanding of this is:
The rotate animation is an animation with the set rotation point (pivotx,pivoty) as the coordinate origin, the clockwise direction from the rotation starting angle (fromdegrees) to the rotational final angle (todegrees).
Where the rotation point defaults to the upper-left corner of the picture is (0,0).
Now write an XML file for rotate animations: rotate_anim.xml
[Java]View PlainCopy
- <?xml version="1.0" encoding="Utf-8"?>
- <rotate xmlns:android="http://schemas.android.com/apk/res/android" >
- <rotate
- android:fromdegrees="0"
- Android:interpolator="@android: Anim/linear_interpolator"//Set the rotation rate, which is set dumpers
- android:pivotx="50%"
- android:pivoty="50%"
- android:todegrees="359"
- Android:duration = " the"
- Android:repeatcount = "-1"
- android:drawable = "@drawable/ic_launcher"
- Android:visible = "true" >
- </rotate>
- </rotate>
Here's what you need to be aware of:
Android:pivotx and Android:pivoty The two property values are float, can give a specific number can also give a percentage. For example, you know the size of the picture is 100, you can give (50,50) the center of rotation point distance picture left 50, right 50
If you do not know the exact size of the picture, you can give the percentage as shown in the code.
The above code also has some properties that are not mentioned in the official API, but these properties can still be used.
Android:interpolator: This property is used to set the rotation rate.
Linearinterpolator is a constant speed effect, accelerateinterpolator for acceleration effect, decelerateinterpolator for deceleration effect,
Android:repeatcount repeat number, default is 0, must be int, can be-1 means no stop
The Android:duration property represents the amount of time, in milliseconds, that is spent turning from android:fromdegrees to Android:todegrees. can be used to calculate speed.
Android:startoffset when the start function is called, in milliseconds, if 10, to start running after 10ms
The Android:repeatmode repeating pattern, which defaults to restart, is re-run from the beginning and can be re-run from the end to reverse.
Effective when Android:repeatcount is greater than 0 or infinite
Android:detachwallpaper indicates whether to run on wallpaper
Android:zadjustment represents the position of the content being animated on the z axis at run time, which defaults to normal. Normal keep content current Z-axis order top runtime display at topmost level bottom runtime
The above attributes, Bo Master pro-Test, can be used normally.
Layout file Activity_main.xml, nothing Special:
[Java]View PlainCopy
- <relativelayout xmlns:android="Http://schemas.android.com/apk/res/android"
- xmlns:tools="Http://schemas.android.com/tools"
- Android:layout_width="Match_parent"
- android:layout_height="Match_parent" >
- <imageview
- android:id="@+id/img"
- android:layout_centerinparent="true"
- Android:layout_width="Wrap_content"
- android:layout_height="Wrap_content"/>
- </RelativeLayout>
Mainactivity.java
[Java]View PlainCopy
- Package com.example.rotateanimation;
- Import android.app.Activity;
- Import Android.os.Bundle;
- Import android.view.animation.Animation;
- Import Android.view.animation.AnimationUtils;
- Import Android.view.animation.LinearInterpolator;
- Import Android.widget.ImageView;
- Public class Mainactivity extends Activity {
- @Override
- protected void OnCreate (Bundle savedinstancestate) {
- super.oncreate (savedinstancestate);
- Setcontentview (R.layout.activity_main);
- Animation rotate = animationutils.loadanimation (this, R.drawable.rotate_anim);
- ((ImageView) Findviewbyid (r.id.img)). setanimation (rotate);
- ((ImageView) Findviewbyid (r.id.img)). startanimation (rotate);
- }
- }
In this way, to run the project, a rotating Android small green man appeared. PS: Do not know how to make a dynamic diagram, the effect can not be displayed.
There are two places in the writing process that need attention:
1, in the Rotate_anim.xml file, the outermost item name is rotate, not set.
[Java]View PlainCopy
- <?xml version="1.0" encoding="Utf-8"?>
- <set xmlns:android="http://schemas.android.com/apk/res/android" >
- <rotate
- android:fromdegrees="0"
- Android:interpolator="@android: Anim/linear_interpolator"//Set the rotation rate, which is set dumpers
- android:pivotx="50%"
- android:pivoty="50%"
- android:todegrees="359"
- Android:duration = " the"
- Android:repeatcount = "-1"
- android:drawable = "@drawable/ic_launcher"
- Android:visible = "true" >
- </rotate>
- </set>
If the code is set, as shown above, the attribute Android:interpolator will be invalidated. The specific reason I did not find, have to know the friend wants to be able to doubt in the blog post, thanks.
In this case, you need to set the animation's interpolator in your code:
[Java]View PlainCopy
- Animation rotate = animationutils.loadanimation (this, R.drawable.rotate_anim);
- Linearinterpolator lin = new Linearinterpolator ();
- Rotate.setinterpolator (Lin);
- ((ImageView) Findviewbyid (r.id.img)). setanimation (rotate);
- ((ImageView) Findviewbyid (r.id.img)). startanimation (rotate);
2. I have tried to set the ImageView src:android:src= "@drawable/rotate_anim" in the layout file directly, the result is that the picture will appear but will not rotate, so do not do so.
3. If the ImageView itself takes the picture, then the drawable attribute set in the rotate is not valid. Will prefer to use ImageView's own pictures
4. When setting the Android:drawable property, do not forget to set android:visible = "true" because it defaults to false (not visible).
In the Java code: rotateanimation A total of three construction methods, here is the third, which is the most parameter. Here is a description of the relevant parameters for rotateanimation in Google's official website:
Most of these parameters have been introduced above, the emphasis is pivotxtype and Pivotytype
int Pivotxtype, animate the X-axis relative to the object position type, with the following pivotxvalue, to determine the center of rotation on the x-axis.
Possible values are: Animation.absolute,animation.relative_to_self, animation.relative_to_parent
If pivotxtype=animation.absolute, this parameter is the value of the x-axis of the center of rotation on the screen;
If pivotxtype=animation.relative_to_parent, the parameter pivotxvalue is a percentage of the horizontal position of the rotation center in the parent control, such as 0.5 for the horizontal middle position of the parent control;
If pivotxtype=animation.relative_to_self, the parameter pivotxvalue is the percentage of the center of rotation in the control's own horizontal position, and if the value of x and Y is set to 0.5, the control rotates in its own center.
All right, sticker code: Activity_main.xml
[Java]View PlainCopy
- <relativelayout xmlns:android="Http://schemas.android.com/apk/res/android"
- xmlns:tools="Http://schemas.android.com/tools"
- Android:layout_width="Match_parent"
- android:layout_height="Match_parent" >
- <imageview
- android:id="@+id/img"
- android:layout_centerinparent="true"
- Android:layout_width="Wrap_content"
- android:layout_height="Wrap_content"
- android:src="@drawable/ic_launcher"/>
- </RelativeLayout>
Mainactivity.java
[Java]View PlainCopy
- Package com.example.rotateanimation;
- Import android.app.Activity;
- Import Android.os.Bundle;
- Import android.view.animation.Animation;
- Import Android.view.animation.AnimationUtils;
- Import Android.view.animation.LinearInterpolator;
- Import android.view.animation.RotateAnimation;
- Import Android.widget.ImageView;
- Public class Mainactivity extends Activity {
- Private ImageView img;
- @Override
- protected void OnCreate (Bundle savedinstancestate) {
- super.oncreate (savedinstancestate);
- Setcontentview (R.layout.activity_main);
- img = (ImageView) Findviewbyid (r.id.img);
- //implemented in XML
- /* Animation rotate = animationutils.loadanimation (this, r.drawable.rotate_anim);
- Linearinterpolator lin = new Linearinterpolator ();
- Rotate.setinterpolator (Lin);
- Img.setanimation (rotate);
- Img.startanimation (rotate); * *
- //Implemented with Java code
- Rotateanimation rotate = new Rotateanimation (0f, 360f, Animation.relative_to_self, 0.5f, animation.relative_to _self, 0.5f);
- Linearinterpolator lin = new Linearinterpolator ();
- Rotate.setinterpolator (Lin);
- Rotate.setduration (); Set the duration of an animation
- Rotate.setrepeatcount (-1); Set number of repetitions
- Rotate.setfillafter (true); Whether the animation stays in the finished state after execution
- Rotate.setstartoffset (ten); Wait time before execution
- Img.setanimation (rotate);
- }
- }
There are many methods of set properties in rotate, which correspond to attribute one by one in XML, which is interesting to try.
Original blog, reproduced please indicate the source
The above is the content of rotateanimation, there is wrong place to welcome correct ~
Click to download the source code
Reference article:
1.http://my.oschina.net/ryanisme/blog/109674
2.http://www.android100.org/html/201304/24/2282.html
3.http://blog.csdn.net/jason0539/article/details/16370405
Rotateanimation of common Android animations