This article mainly introduces how to use rotate to achieve the effect of continuous rotation of images in Android . Android platform provides two kinds of animation, one is Tween animation, that is, through the scene of the object constantly do image transformation (translation, scaling, rotation) to produce animation effect; the second type is Frame animation, that is, the sequential playback of pre-done images, similar to the movie. This paper analyzes the rotate of tween animation to realize the rotation effect.
In the Sina Weibo client in the operation of the various operations in the upper right corner of the activity will have a rotating icon, similar to the effect of refreshing, to the user to operate the prompt. This non-modal hint is recommended to use, then share below how to achieve this effect.
1. Define a ImageView
Define a ImageView to load the picture, where the picture will be rotate used for rotation, other view also.
Resource file is
Java code
- <?xml version= "1.0" encoding= "Utf-8"?>
- <linearlayout
- Xmlns:android= "Http://schemas.android.com/apk/res/android"
- Android:layout_width= "Match_parent"
- android:layout_height= "Match_parent" >
- <imageview
- Android:id= "@+id/infooperating"
- Android:layout_width= "Wrap_content"
- android:layout_height= "Wrap_content"
- android:src= "@drawable/operating"
- Android:scaletype= "Center" >
- </ImageView>
- </LinearLayout>
The ANDROID:SRC is the picture content, you can use the picture in the attachment.
The Java code is
Java code ImageView infooperatingiv = (ImageView) Findviewbyid (r.id.infooperating);
2. Define rotate rotation effect
Create a new Tip.xml file under the Res/anim folder with the following contents
Java code
- <?xml version= "1.0" encoding= "Utf-8"?>
- <set xmlns:android= "Http://schemas.android.com/apk/res/android" >
- <rotate
- android:fromdegrees= "0"
- android:todegrees= "359"
- android:duration= "500"
- Android:repeatcount= "-1"
- android:pivotx= "50%"
- android:pivoty= "50%"/>
- </set>
The meaning is that the rotation starts at 0-359 degrees, 0-359 (if set to 360 pauses at stop) the rotation takes 500ms, the center of rotation is 50% distance from the left vertex of the view, and 50% distance from the top edge of the view, which is the positive center, For each of these meanings, see the specific properties described below.
The Java code is
Java code
- Animation Operatinganim = Animationutils.loadanimation (this, r.anim.tip);
- Linearinterpolator lin = new Linearinterpolator ();
- Operatinganim.setinterpolator (Lin);
The setinterpolator indicates the set rotation rate. Linearinterpolator for the uniform effect, accelerateinterpolator for acceleration effect, decelerateinterpolator for deceleration effect, specifically visible below android:interpolator the introduction.
A. The meanings of the attributes are as follows (the red ones are noted):
android:fromdegrees The starting angle of the degree
android:todegrees The angle of the end of the degree, negative number is counterclockwise, positive indicates clockwise. If 10 laps is 3600 larger than android:fromdegrees
X-coordinate of the android:pivotx Rotation Center
Floating-point numbers or percentages. The floating-point number represents the left edge relative to object, such as 5; The percentage represents the left edge of the object, such as 5%; Another percentage represents the left edge of the parent container, such as 5%p; A general setting of 50% indicates the object center
android:pivoty y-coordinate of the center of rotation
Floating-point numbers or percentages. A floating-point number indicates a relative to the top edge of object, such as 5; The percentage represents the top edge of the object, such as 5%; Another percentage represents the top edge of the parent container, such as 5%p; A general setting of 50% indicates the object center
Android:duration represents the amount of time, in milliseconds, that is spent turning from android:fromdegrees to Android:todegrees. can be used to calculate speed.
Android:interpolator represents the rate of change, but not the speed of Operation . An interpolation property that sets the animation effect to acceleration, deceleration, repetition, bounce, and so on. The default is start and end slow middle fast,
Android:startoffset When the start function is called, in milliseconds, if 10, to start running after 10ms
Android:repeatcount Repeat number, default is 0, must be int, can be-1 means no stop
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 maintains the current z-axis order of content
Top runtime is displayed at the top level
Bottom runtime is displayed at the lowest level
B. Operating speed
The run speed is run time (android:duration) divided by the Run angle difference (android:todegrees-android:fromdegrees), such as Android:duration for 1000,android: Todegrees for 360,android:fromdegrees 0 means 1 seconds to 1 turns.
C. Cyclic operation
Java code
- android:fromdegrees= "0"
- Android:todegrees= "360"
- Android:repeatcount= "-1"
Android:repeatcount= "-1" means the loop runs, with android:fromdegrees= "0" android:todegrees= "360" for uninterrupted
3. Start and stop rotation
Called before the operation begins
Java code
- if (Operatinganim! = null) {
- Infooperatingiv.startanimation (Operatinganim);
- }
Called when the operation is complete
Java code
- Infooperatingiv.clearanimation ();
Many friends do not know how to stop rotating animation, so force set rotate to rotate how many laps to represent the operation, but can not match the actual progress of the operation, in fact as long as the above code to clear animation.
Other:
The problem with the above rotation in the horizontal screen (set to not redraw the activity) is the Rotation Center offset, which causes the animation rotation to deviate from the original rotation center . Resolved as follows
Java code
- @Override
- public void onconfigurationchanged (Configuration newconfig) {
- Super.onconfigurationchanged (Newconfig);
- if (Operatinganim! = NULL && INFOOPERATINGIV! = null && operatinganim.hasstarted ()) {
- Infooperatingiv.clearanimation ();
- Infooperatingiv.startanimation (Operatinganim);
- }
- }
Android Rotate animation around center