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
    1. 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="
  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

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
    1. android:fromdegrees="0"
    2. android:todegrees=" the"
    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.   
  4. Super. onconfigurationchanged (Newconfig);
  5.   
  6. if (operatinganim! = null && infooperatingiv! = null && operatingAnim.h Asstarted ()) {
  7. infooperatingiv.clearanimation ();
  8. infooperatingiv.startanimation (Operatinganim);
  9.     }  

Android Rotate animation around center

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.