It mainly introduces how to use rotate in Android to achieve the effect of constantly rotating pictures. The Android platform provides two types of animations. One is Tween animation, which produces animation effects by continuously transforming (translation, scaling, and rotating) objects in the scene; the second is Frame animation, which means playing pre-made images in sequence, similar to movies. This article analyzes the rotation effect of Tween animation.
When each operation is in progress in the Sina Weibo client, there will be an icon that keeps rotating in the upper right corner of the activity, similar to the refresh effect, giving users prompts during operation. This non-modal prompt method is recommended, so let’s share how to achieve this effect below
1. Define an ImageView
Defining an ImageView is to load images, the images will be rotated by rotate, and other Views are also available.
The resource file is
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:andro
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:
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, and you can use the pictures in the attachment.
The java code is
ImageView infoOperatingIV = (ImageView)findViewById();
2. Define the rotation effect of rotate
Create a new file in the res/anim folder, the content is as follows
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:andro>
<rotate
android:fromDegrees="0"
android:toDegrees="359"
android:duration="500"
android:repeatCount="-1"
android:pivotX="50%"
android:pivotY="50%" />
</set>
The meaning means that the rotation starts from 0 to 359 degrees. The rotation time is 500ms for 0-359 (if set to 360, there will be a pause when it stops). The rotation center is 50% away from the left vertex of the view, and the distance from the upper edge of the view is 50%, that is, the positive center. For each meaning, see the specific attribute introduction below.
The java code is
Animation operatingAnim = (this, );
LinearInterpolator lin = new LinearInterpolator();
(lin);
setInterpolator indicates the setting of the rotation rate. LinearInterpolator is a constant speed effect, Accelerateinterpolator is an acceleration effect, and DecelerateInterpolator is a decelerating effect. For details, see the introduction of android:interpolator below.
a. The meaning of the attributes in it is as follows (note the red part):
android:fromDegreesThe starting angle degree
android:toDegreesThe ending angle degree, negative number means counterclockwise, positive number means clockwise. If 10 circles, it will be 3600 larger than android:fromDegrees
android:pivotXX coordinate of rotation center
Floating point number or percentage. Float number represents the left edge relative to the Object, such as 5; percentage represents the left edge relative to the Object, such as 5%; another percentage represents the left edge relative to the parent container, such as 5%p; generally set to 50% means in the center of the Object
android:pivotYY coordinate of rotation center
Floating point number or percentage. Float number represents the upper edge relative to the Object, such as 5; percentage represents the upper edge relative to the Object, such as 5%; another percentage represents the upper edge relative to the parent container, such as 5%p; generally set to 50% means in the center of the Object
android:durationIndicates the time it takes to rotate from android:fromDegrees to android:toDegrees in milliseconds. Can be used to calculate speed.
android:interpolatorIndicates the rate of change, but not the speed of operation. An interpolation property can set the animation effect to acceleration, deceleration, repetition, rebound, etc. The default is to start and end slow and the middle fast.
android:startOffsetThe time to wait for the start function to start running after calling the start function is milliseconds. If it is 10, it means that the running starts after 10ms
android:repeatCount The number of repeated times, the default is 0, must be int, which can be -1 to indicate that it does not stop
android:repeatMode The repetitive mode is restart by default, that is, start to run again, and can be reverse, that is, start to run forward from the end. Effective when android:repeatCount is greater than 0 or is infinite
android:detachWallpaperIndicates whether it runs on the wallpaper
android:zAdjustmentIndicates the position of the animated content on the z-axis at runtime, and defaults to normal.
Normal keeps the current z-axis order of content
Top runtime displays at the top level
The bottom is displayed at the bottom when running
b. Running speed
The running speed is the running time (android:duration) divided by the running angle difference (android:toDegrees-android:fromDegrees). For example, android:duration is 1000, and android:toDegrees is 360, and android:fromDegrees is 0, which means 1 second and 1 turn.
c. Loop running
android:fromDegrees="0"
android:toDegrees="360"
android:repeatCount="-1"
android:repeatCount="-1" means loop running, and combined with android:fromDegrees="0" android:toDegrees="360" means uninterrupted
3. Start and stop rotation
Called before the operation starts
if (operatingAnim != null) {
(operatingAnim);
}
Called when the operation is completed
();
Many friends don’t know how to stop the rotation animation, so they force the rotation to rotate how many times it turns indicates the operation, but it cannot match the actual progress of the operation. In fact, just clear the animation as shown in the above code.
other:
For the above rotation, there will be a problem when the horizontal screen (set to not repaint the activity), that is, the rotation center is offset, causing the animation rotation to deviate from the original rotation center. Solved as follows
@Override
public void onConfigurationChanged(Configuration newConfig) {
(newConfig);
if (operatingAnim != null && infoOperatingIV != null && ()) {
();
(operatingAnim);
}
}
When each operation is in progress in the Sina Weibo client, there will be an icon that keeps rotating in the upper right corner of the activity, similar to the refresh effect, giving users prompts during operation. This non-modal prompt method is recommended, so let’s share how to achieve this effect below
1. Define an ImageView
Defining an ImageView is to load images, the images will be rotated by rotate, and other Views are also available.
The resource file is
Copy the codeThe code is as follows:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:andro
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:
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, and you can use the pictures in the attachment.
The java code is
Copy the codeThe code is as follows:
ImageView infoOperatingIV = (ImageView)findViewById();
2. Define the rotation effect of rotate
Create a new file in the res/anim folder, the content is as follows
Copy the codeThe code is as follows:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:andro>
<rotate
android:fromDegrees="0"
android:toDegrees="359"
android:duration="500"
android:repeatCount="-1"
android:pivotX="50%"
android:pivotY="50%" />
</set>
The meaning means that the rotation starts from 0 to 359 degrees. The rotation time is 500ms for 0-359 (if set to 360, there will be a pause when it stops). The rotation center is 50% away from the left vertex of the view, and the distance from the upper edge of the view is 50%, that is, the positive center. For each meaning, see the specific attribute introduction below.
The java code is
Copy the codeThe code is as follows:
Animation operatingAnim = (this, );
LinearInterpolator lin = new LinearInterpolator();
(lin);
setInterpolator indicates the setting of the rotation rate. LinearInterpolator is a constant speed effect, Accelerateinterpolator is an acceleration effect, and DecelerateInterpolator is a decelerating effect. For details, see the introduction of android:interpolator below.
a. The meaning of the attributes in it is as follows (note the red part):
android:fromDegreesThe starting angle degree
android:toDegreesThe ending angle degree, negative number means counterclockwise, positive number means clockwise. If 10 circles, it will be 3600 larger than android:fromDegrees
android:pivotXX coordinate of rotation center
Floating point number or percentage. Float number represents the left edge relative to the Object, such as 5; percentage represents the left edge relative to the Object, such as 5%; another percentage represents the left edge relative to the parent container, such as 5%p; generally set to 50% means in the center of the Object
android:pivotYY coordinate of rotation center
Floating point number or percentage. Float number represents the upper edge relative to the Object, such as 5; percentage represents the upper edge relative to the Object, such as 5%; another percentage represents the upper edge relative to the parent container, such as 5%p; generally set to 50% means in the center of the Object
android:durationIndicates the time it takes to rotate from android:fromDegrees to android:toDegrees in milliseconds. Can be used to calculate speed.
android:interpolatorIndicates the rate of change, but not the speed of operation. An interpolation property can set the animation effect to acceleration, deceleration, repetition, rebound, etc. The default is to start and end slow and the middle fast.
android:startOffsetThe time to wait for the start function to start running after calling the start function is milliseconds. If it is 10, it means that the running starts after 10ms
android:repeatCount The number of repeated times, the default is 0, must be int, which can be -1 to indicate that it does not stop
android:repeatMode The repetitive mode is restart by default, that is, start to run again, and can be reverse, that is, start to run forward from the end. Effective when android:repeatCount is greater than 0 or is infinite
android:detachWallpaperIndicates whether it runs on the wallpaper
android:zAdjustmentIndicates the position of the animated content on the z-axis at runtime, and defaults to normal.
Normal keeps the current z-axis order of content
Top runtime displays at the top level
The bottom is displayed at the bottom when running
b. Running speed
The running speed is the running time (android:duration) divided by the running angle difference (android:toDegrees-android:fromDegrees). For example, android:duration is 1000, and android:toDegrees is 360, and android:fromDegrees is 0, which means 1 second and 1 turn.
c. Loop running
Copy the codeThe code is as follows:
android:fromDegrees="0"
android:toDegrees="360"
android:repeatCount="-1"
android:repeatCount="-1" means loop running, and combined with android:fromDegrees="0" android:toDegrees="360" means uninterrupted
3. Start and stop rotation
Called before the operation starts
Copy the codeThe code is as follows:
if (operatingAnim != null) {
(operatingAnim);
}
Called when the operation is completed
Copy the codeThe code is as follows:
();
Many friends don’t know how to stop the rotation animation, so they force the rotation to rotate how many times it turns indicates the operation, but it cannot match the actual progress of the operation. In fact, just clear the animation as shown in the above code.
other:
For the above rotation, there will be a problem when the horizontal screen (set to not repaint the activity), that is, the rotation center is offset, causing the animation rotation to deviate from the original rotation center. Solved as follows
Copy the codeThe code is as follows:
@Override
public void onConfigurationChanged(Configuration newConfig) {
(newConfig);
if (operatingAnim != null && infoOperatingIV != null && ()) {
();
(operatingAnim);
}
}