Android Rotate animation around center

Source: Internet
Author: User

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
  1. <?xml version= "1.0" encoding= "Utf-8"?>
  2. <linearlayout
  3. Xmlns:android= "Http://schemas.android.com/apk/res/android"
  4. Android:layout_width= "Match_parent"
  5. android:layout_height= "Match_parent" >
  6. <imageview
  7. Android:id= "@+id/infooperating"
  8. Android:layout_width= "Wrap_content"
  9. android:layout_height= "Wrap_content"
  10. android:src= "@drawable/operating"
  11. Android:scaletype= "Center" >
  12. </ImageView>
  13. </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 
    1. <?xml version= "1.0" encoding= "Utf-8"?>
    2. <set xmlns:android= "Http://schemas.android.com/apk/res/android" >
    3. <rotate
    4. android:fromdegrees= "0"
    5. android:todegrees= "359"
    6. android:duration= "500"
    7. Android:repeatcount= "-1"
    8. android:pivotx= "50%"
    9. android:pivoty= "50%"/>
    10. </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 
    1. Animation Operatinganim = Animationutils.loadanimation (this, r.anim.tip);
    2. Linearinterpolator lin = new Linearinterpolator ();
    3. 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 
    1. android:fromdegrees= "0"
    2. Android:todegrees= "360"
    3. 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 
    1. if (Operatinganim! = null) {
    2. Infooperatingiv.startanimation (Operatinganim);
    3. }

Called when the operation is complete

Java code 
    1. 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 
    1. @Override
    2. public void onconfigurationchanged (Configuration newconfig) {
    3. Super.onconfigurationchanged (Newconfig);
    4. if (Operatinganim! = NULL && INFOOPERATINGIV! = null && operatinganim.hasstarted ()) {
    5. Infooperatingiv.clearanimation ();
    6. Infooperatingiv.startanimation (Operatinganim);
    7. }
    8. }

Android Rotate animation around center

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.