Rotateanimation of common Android animations

Source: Internet
Author: User

Two days ago received a task to do a UI, useful to the animation, so the time to see the next Android animation related knowledge.

There are four types of Android animation, namely

Alpha Transparency Animation

Scale size Scaling Animation

Translate Displacement Animation

Rotate Rotate Animation

These four types of animations can be categorized by pattern:

tweened Animation (gradient animation)--alpha and scale

Frame by frame (picture conversion animation)--translate and rotate

Tell me about the various properties of the rotate animation I've learned:

In the XML:

The official rotate attribute is given as shown above.

Android:drawable pictures that need to be animated for rotation

The starting point of the android:fromdegrees rotation (the angle at which the rotation begins)

Android:todegrees end point of rotation (rotation final angle)

Andoird:pivotx x value of the rotation point (offset from the left)

Android:pivoty y value of the rotation point (offset from the top)

Android:visible this good understanding, is the initial display state of the picture

My understanding of this is:

The rotate animation is an animation with the set rotation point (pivotx,pivoty) as the coordinate origin, the clockwise direction from the rotation starting angle (fromdegrees) to the rotational final angle (todegrees).

Where the rotation point defaults to the upper-left corner of the picture is (0,0).

Now write an XML file for rotate animations: rotate_anim.xml

[Java]View PlainCopy
  1. <?xml version="1.0" encoding="Utf-8"?>
  2. <rotate xmlns:android="http://schemas.android.com/apk/res/android" >
  3. <rotate
  4. android:fromdegrees="0"
  5. Android:interpolator="@android: Anim/linear_interpolator"//Set the rotation rate, which is set dumpers
  6. android:pivotx="50%"
  7. android:pivoty="50%"
  8. android:todegrees="359"
  9. Android:duration = " the"
  10. Android:repeatcount = "-1"
  11. android:drawable = "@drawable/ic_launcher"
  12. Android:visible = "true" >
  13. </rotate>
  14. </rotate>

Here's what you need to be aware of:

Android:pivotx and Android:pivoty The two property values are float, can give a specific number can also give a percentage. For example, you know the size of the picture is 100, you can give (50,50) the center of rotation point distance picture left 50, right 50

If you do not know the exact size of the picture, you can give the percentage as shown in the code.

The above code also has some properties that are not mentioned in the official API, but these properties can still be used.

Android:interpolator: This property is used to set the rotation rate.

Linearinterpolator is a constant speed effect, accelerateinterpolator for acceleration effect, decelerateinterpolator for deceleration effect,

Android:repeatcount repeat number, default is 0, must be int, can be-1 means no stop

The Android:duration property represents the amount of time, in milliseconds, that is spent turning from android:fromdegrees to Android:todegrees. can be used to calculate speed.

Android:startoffset when the start function is called, in milliseconds, if 10, to start running after 10ms

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 keep content current Z-axis order top runtime display at topmost level bottom runtime

The above attributes, Bo Master pro-Test, can be used normally.

Layout file Activity_main.xml, nothing Special:

[Java]View PlainCopy
  1. <relativelayout xmlns:android="Http://schemas.android.com/apk/res/android"
  2. xmlns:tools="Http://schemas.android.com/tools"
  3. Android:layout_width="Match_parent"
  4. android:layout_height="Match_parent" >
  5. <imageview
  6. android:id="@+id/img"
  7. android:layout_centerinparent="true"
  8. Android:layout_width="Wrap_content"
  9. android:layout_height="Wrap_content"/>
  10. </RelativeLayout>

Mainactivity.java

[Java]View PlainCopy
  1. Package com.example.rotateanimation;
  2. Import android.app.Activity;
  3. Import Android.os.Bundle;
  4. Import android.view.animation.Animation;
  5. Import Android.view.animation.AnimationUtils;
  6. Import Android.view.animation.LinearInterpolator;
  7. Import Android.widget.ImageView;
  8. Public class Mainactivity extends Activity {
  9. @Override
  10. protected void OnCreate (Bundle savedinstancestate) {
  11. super.oncreate (savedinstancestate);
  12. Setcontentview (R.layout.activity_main);
  13. Animation rotate = animationutils.loadanimation (this, R.drawable.rotate_anim);
  14. ((ImageView) Findviewbyid (r.id.img)). setanimation (rotate);
  15. ((ImageView) Findviewbyid (r.id.img)). startanimation (rotate);
  16. }
  17. }



In this way, to run the project, a rotating Android small green man appeared. PS: Do not know how to make a dynamic diagram, the effect can not be displayed.

There are two places in the writing process that need attention:

1, in the Rotate_anim.xml file, the outermost item name is rotate, not set.

[Java]View PlainCopy
  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:interpolator="@android: Anim/linear_interpolator"//Set the rotation rate, which is set dumpers
  6. android:pivotx="50%"
  7. android:pivoty="50%"
  8. android:todegrees="359"
  9. Android:duration = " the"
  10. Android:repeatcount = "-1"
  11. android:drawable = "@drawable/ic_launcher"
  12. Android:visible = "true" >
  13. </rotate>
  14. </set>

If the code is set, as shown above, the attribute Android:interpolator will be invalidated. The specific reason I did not find, have to know the friend wants to be able to doubt in the blog post, thanks.

In this case, you need to set the animation's interpolator in your code:

[Java]View PlainCopy
    1. Animation rotate = animationutils.loadanimation (this, R.drawable.rotate_anim);
    2. Linearinterpolator lin = new Linearinterpolator ();
    3. Rotate.setinterpolator (Lin);
    4. ((ImageView) Findviewbyid (r.id.img)). setanimation (rotate);
    5. ((ImageView) Findviewbyid (r.id.img)). startanimation (rotate);

2. I have tried to set the ImageView src:android:src= "@drawable/rotate_anim" in the layout file directly, the result is that the picture will appear but will not rotate, so do not do so.

3. If the ImageView itself takes the picture, then the drawable attribute set in the rotate is not valid. Will prefer to use ImageView's own pictures


4. When setting the Android:drawable property, do not forget to set android:visible = "true" because it defaults to false (not visible).

In the Java code: rotateanimation A total of three construction methods, here is the third, which is the most parameter. Here is a description of the relevant parameters for rotateanimation in Google's official website:

Most of these parameters have been introduced above, the emphasis is pivotxtype and Pivotytype

int Pivotxtype, animate the X-axis relative to the object position type, with the following pivotxvalue, to determine the center of rotation on the x-axis.

Possible values are: Animation.absolute,animation.relative_to_self, animation.relative_to_parent

If pivotxtype=animation.absolute, this parameter is the value of the x-axis of the center of rotation on the screen;

If pivotxtype=animation.relative_to_parent, the parameter pivotxvalue is a percentage of the horizontal position of the rotation center in the parent control, such as 0.5 for the horizontal middle position of the parent control;
If pivotxtype=animation.relative_to_self, the parameter pivotxvalue is the percentage of the center of rotation in the control's own horizontal position, and if the value of x and Y is set to 0.5, the control rotates in its own center.

All right, sticker code: Activity_main.xml

[Java]View PlainCopy
  1. <relativelayout xmlns:android="Http://schemas.android.com/apk/res/android"
  2. xmlns:tools="Http://schemas.android.com/tools"
  3. Android:layout_width="Match_parent"
  4. android:layout_height="Match_parent" >
  5. <imageview
  6. android:id="@+id/img"
  7. android:layout_centerinparent="true"
  8. Android:layout_width="Wrap_content"
  9. android:layout_height="Wrap_content"
  10. android:src="@drawable/ic_launcher"/>
  11. </RelativeLayout>

Mainactivity.java

[Java]View PlainCopy
  1. Package com.example.rotateanimation;
  2. Import android.app.Activity;
  3. Import Android.os.Bundle;
  4. Import android.view.animation.Animation;
  5. Import Android.view.animation.AnimationUtils;
  6. Import Android.view.animation.LinearInterpolator;
  7. Import android.view.animation.RotateAnimation;
  8. Import Android.widget.ImageView;
  9. Public class Mainactivity extends Activity {
  10. Private ImageView img;
  11. @Override
  12. protected void OnCreate (Bundle savedinstancestate) {
  13. super.oncreate (savedinstancestate);
  14. Setcontentview (R.layout.activity_main);
  15. img = (ImageView) Findviewbyid (r.id.img);
  16. //implemented in XML
  17. /* Animation rotate = animationutils.loadanimation (this, r.drawable.rotate_anim);
  18. Linearinterpolator lin = new Linearinterpolator ();
  19. Rotate.setinterpolator (Lin);
  20. Img.setanimation (rotate);
  21. Img.startanimation (rotate); * *
  22. //Implemented with Java code
  23. Rotateanimation rotate = new Rotateanimation (0f, 360f, Animation.relative_to_self, 0.5f, animation.relative_to  _self, 0.5f);
  24. Linearinterpolator lin = new Linearinterpolator ();
  25. Rotate.setinterpolator (Lin);
  26. Rotate.setduration (); Set the duration of an animation
  27. Rotate.setrepeatcount (-1); Set number of repetitions
  28. Rotate.setfillafter (true); Whether the animation stays in the finished state after execution
  29. Rotate.setstartoffset (ten); Wait time before execution
  30. Img.setanimation (rotate);
  31. }
  32. }

There are many methods of set properties in rotate, which correspond to attribute one by one in XML, which is interesting to try.

Original blog, reproduced please indicate the source

The above is the content of rotateanimation, there is wrong place to welcome correct ~

Click to download the source code

Reference article:

1.http://my.oschina.net/ryanisme/blog/109674

2.http://www.android100.org/html/201304/24/2282.html

3.http://blog.csdn.net/jason0539/article/details/16370405

Rotateanimation of common Android animations

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.