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="
- 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
run at run time (android:duration) divided by 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=" the"
- 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.h Asstarted ()) {
- infooperatingiv.clearanimation ();
- infooperatingiv.startanimation (Operatinganim);
- }
- }
Android Rotate animation around center