Android Tween animation-RotateAnimation

Source: Internet
Author: User

This article mainly introduces how to use rotate in Android to achieve non-stop image rotation. The Android platform provides two types of animations: Tween animation, that is, animation effects are generated by constantly performing image transformations (translation, scaling, and rotation) on the objects in the scenario; the second type is Frame Animation, that is, sequential playback of prepared images, similar to movies. This article analyzes the rotation effect of the Tween animation.

During each operation in the Sina Weibo client, there will be a non-stop rotation icon in the upper right corner of the activity, similar to the refreshing effect, giving the user a prompt in the operation. This kind of non-modal prompt method is recommended for use, so let's share how to achieve this effect.

1. Define an ImageView
Define an ImageView to load the image. The image will be rotated by rotate, and other views can also be used.
The resource file isCopy codeThe Code is as follows: <? 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>

Android: src indicates the image content. You can use the image in the attachment.
The java code isCopy codeThe Code is as follows: ImageView infoOperatingIV = (ImageView) findViewById (R. id. infoOperating );

2. Define the rotate rotation effect
Create the tip. xml file in the res/anim folder. The content is as follows:Copy codeThe Code is as follows: <? 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: Required Tx = "50%"
Android: Ty = "50%"/>
</Set>

It indicates that the rotation starts from 0 to 359 degrees, and the rotation interval between 0 and 359 degrees is 360 ms. If it is set to 500, a pause occurs when it is stopped, the center of rotation is 50% away from the left vertex of the view, and 50% away from the top edge of the view, that is, the center of the center. For details about each meaning, see the specific attributes below.

The java code isCopy codeThe Code is as follows: Animation operatingAnim = AnimationUtils. loadAnimation (this, R. anim. tip );
LinearInterpolator lin = new LinearInterpolator ();
OperatingAnim. setInterpolator (lin );

SetInterpolator indicates setting the rotation rate. LinearInterpolator is a constant speed, Accelerateinterpolator is a acceleration effect, and DecelerateInterpolator is a deceleration effect. For details, see the android: interpolator introduction.
A. the attribute meanings are as follows (note the red part ):
Android: fromDegrees
Starting angle level
Android: toDegreesDegrees of the end angle. A negative value indicates a counter-clockwise value. A positive value indicates a clockwise value. For example, 10 laps is 3600 larger than android: fromDegrees.
Android: strongtxX coordinate of the center of rotation
Floating Point number or percentage. Floating Point number indicates the left edge relative to the Object, for example, 5; Percentage indicates the left edge relative to the Object, for example, 5%; another percentage indicates the left edge relative to the parent container, such as 5% p; generally, the value 50% indicates that
Android: polictyY coordinate of the rotation center
Floating Point number or percentage. Floating Point number indicates the upper edge relative to the Object, such as 5; Percentage indicates the upper edge relative to the Object, such as 5%; another percentage indicates the upper edge relative to the parent container, such as 5% p; generally, the value 50% indicates that
Android: durationIt indicates the time taken to rotate from android: fromDegrees to android: toDegrees, in milliseconds. It can be used to calculate the speed.
Android: interpolatorIndicates the change rate, but not the running speed. An Interpolation attribute that allows you to set the animation effect to acceleration, deceleration, repetition, and bounce. The default value is slow between start and end,
Android: startOffsetThe time to wait for the start operation after the start function is called. The unit is milliseconds. If the value is 10, the operation starts after 10 ms.
Android: repeatCount repeated times. The default value is 0. It must be an int value. The value-1 indicates that the repeatCount is not stopped.
Android: repeatMode, restart by default, that is, re-run with the re-header. You can re-run the reverse from the end. Effective when android: repeatCount is greater than 0 or infinite
Android: detachWallpaperIndicates whether to run on the wallpaper
Android: zAdjustmentIt indicates the position of the content to be animated on the Z axis during running. The default value is normal.
Normal maintains the current z-axis order of the content
Top display at the top layer during running
Bottom is displayed at the bottom layer during 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, android: toDegrees is 360, android: if fromDegrees is 0, it indicates 1 second to 1 lap.
C. run cyclically Copy codeThe Code is as follows: android: fromDegrees = "0"
Android: toDegrees = "360"
Android: repeatCount = "-1"

Android: repeatCount = "-1" indicates cyclic operation. In combination with android: fromDegrees = "0" android: toDegrees = "360", this indicates uninterrupted operation.
3. Start and Stop Rotation
Called before the operation startsCopy codeThe Code is as follows: if (operatingAnim! = Null ){
InfoOperatingIV. startAnimation (operatingAnim );
}

Called when the operation is completeCopy codeThe Code is as follows: infoOperatingIV. clearAnimation ();

Many friends do not know how to stop the rotation of animation, so they force the number of turns in the rotate to indicate the operation, but it cannot match the actual progress of the operation. In fact, they only need to clear the animation as shown in the code above.
Others:
An error occurs when the above rotation occurs on the horizontal screen (set to not repaint the activity), that is, the rotation center offset, resulting in the animation rotation deviation from the original rotation center. The solution is as follows:Copy codeThe Code is as follows: @ Override
Public void onConfigurationChanged (Configuration newConfig ){
Super. onConfigurationChanged (newConfig );
If (operatingAnim! = Null & infoOperatingIV! = Null & operatingAnim. hasStarted ()){
InfoOperatingIV. clearAnimation ();
InfoOperatingIV. startAnimation (operatingAnim );
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.