Android Tween Animation rotateanimation implementation of the picture rotation effect of the example introduction _android

Source: Internet
Author: User
This paper mainly introduces how to use rotate to realize the effect of rotating picture in Android. Android platform provides two kinds of animation, one is Tween animation, that is, through the scene of the object constantly doing image transformation (translation, scaling, rotation) to produce animation effect; the second category is Frame animation, which is the sequential playback of the image in advance, similar to the movie. This paper analyzes the rotate of tween animation to realize the rotation effect.

In the Sina Weibo client each operation in the upper-right corner of the activity will have a rotating icon, similar to the effect of refreshing, to the user in action prompts. This modeless hint is recommended for use, so share below how to achieve this effect.

1. Define a ImageView
The definition of a imageview is to load the picture, where the picture will be rotate for rotation, and other view can be.
Resource file is
Copy Code code 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>

The ANDROID:SRC is the picture content, you can use the picture in the attachment.
Java code is
Copy Code code as follows:

ImageView infooperatingiv = (imageview) Findviewbyid (r.id.infooperating);

2, the definition of rotate rotation effect
Create a new Tip.xml file under the Res/anim folder, which reads
Copy Code code 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:pivotx= "50%"
android:pivoty= "50%"/>
</set>

The meaning of the rotation starting from 0-359 degrees, 0-359 (if set to 360 at the end of the stop phenomenon) the degree of rotation time is 500ms, the rotation center distance from the view of the left vertex is 50% distance, from the top edge of view is 50% distance, that is, the center, For each meaning, see the specific properties described below.

Java code is
Copy Code code as follows:

Animation Operatinganim = Animationutils.loadanimation (this, r.anim.tip);
Linearinterpolator lin = new Linearinterpolator ();
Operatinganim.setinterpolator (Lin);

Setinterpolator indicates the rotation rate is set. Linearinterpolator for the uniform effect, accelerateinterpolator for the acceleration effect, decelerateinterpolator for deceleration effect, concrete visible below android:interpolator introduction.
A. The meaning of the attributes is as follows (note in the Red section):
Android:fromdegrees
Degrees of the starting angle
android:todegreesThe angle of the end degrees, negative numbers are counterclockwise, positive numbers for clockwise. If 10 laps is bigger than android:fromdegrees 3600 can
Android:pivotxx coordinate of the center of rotation
Floating-point number or percent. The floating-point number represents the left edge relative to object, such as 5; The percentage is expressed relative to the left edge of object, such as 5%; Another percentage indicates the relative to the left edge of the parent container, such as 5%p; A general setting of 50% means the object center
Android:pivotyThe y-coordinate of the center of rotation
Floating-point number or percent. A floating-point number represents the upper edge of object, such as 5; The percentage is expressed relative to the top edge of object, such as 5%; Another percentage represents the upper edge of the parent container, such as 5%p; A general setting of 50% means the object center
android:durationRepresents the amount of time, in milliseconds, it takes to rotate from android:fromdegrees to android:todegrees. can be used to calculate speed.
Android:interpolatorRepresents the rate of change, but not the speed at which it is run. A interpolation property, you can set the animation effect to speed, deceleration, repetition, bounce and so on. The default is start and end slow middle fast,
Android:startoffsetThe time, in milliseconds, to wait until the start function is called, or 10, to begin running after 10ms
Android:repeatcount the number of repetitions, the default is 0, must be int, can be-1 means not to stop
Android:repeatmode repeat mode, default to restart, that is, start over again, can be reverse that is, from the end of the run forward again. Effective when Android:repeatcount is greater than 0 or infinite
Android:detachwallpaperIndicates whether to run on the wallpaper
android:zadjustmentRepresents the position of the animated content at run time on the z-axis, which defaults to normal.
Normal retains the current z-axis order of content
Top run is displayed at the topmost level
Bottom runtime display at the bottom
B. Speed of Operation
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 cycles.
c. Circular Operation
Copy Code code as follows:

android:fromdegrees= "0"
Android:todegrees= "360"
Android:repeatcount= "-1"

Android:repeatcount= "-1" means that the cycle runs, with the android:fromdegrees= "0" android:todegrees= "360" means uninterrupted
3. Start and stop rotation
Called before the operation starts
Copy Code code as follows:

if (Operatinganim!= null) {
Infooperatingiv.startanimation (Operatinganim);
}

Called when the operation completes
Copy Code code as follows:

Infooperatingiv.clearanimation ();

Many friends do not know how to stop the rotation animation, so forced to set rotate rotation number of the operation, but can not match the actual progress of the operation, in fact, as long as the code shown in the clear animation.
Other:
The problem with the above rotation in the horizontal screen (which is set to not redraw the activity) is the rotation center offset, which causes the animation to rotate away from the original rotation center. Resolved as follows
Copy Code code as follows:

@Override
public void onconfigurationchanged (Configuration newconfig) {
super.onconfigurationchanged (newconfig);
if (Operatinganim!= null && infooperatingiv!= null && operatinganim.hasstarted ()) {
Infooperat Ingiv.clearanimation ();
Infooperatingiv.startanimation (Operatinganim);
}
}
Related Article

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.