1. 程式人生 > >Android 常用動畫之RotateAnimation

Android 常用動畫之RotateAnimation

找到 draw 狀態 set 運行 代碼 pla instance from

前兩天接到任務做一個UI,有用到動畫,於是抽空看了下Android動畫相關知識。

Android Animation共有四大類型,分別是

Alpha 透明度動畫

Scale 大小伸縮動畫

Translate 位移動畫

Rotate 旋轉動畫

這四類動畫按模式又可分為:

tweened animation(漸變動畫) —— alpha 與 scale

frame by frame(畫面轉換動畫) —— translate 與 rotate

講一下我所了解到的rotate動畫的各個屬性:

在XML中:

技術分享

官方給予的Rotate屬性如上所示。

android:drawable 需要進行旋轉動畫的圖片

android:fromDegrees 旋轉的起始點(旋轉開始的角度)

android:toDegrees 旋轉的結束點(旋轉最終角度)

andoird:pivotX 旋轉點的X值(距離左側的偏移量)

android:pivotY旋轉點的Y值(距離頂部的偏移量)

android: visible這個好理解,就是圖片初始的顯示狀態

我對這個的理解是:

rotate動畫是以設置的旋轉點(pivotX,pivotY)為坐標原點,順時針方向從旋轉起始角度(fromDegrees)到旋轉最終角度(toDegrees)的動畫,

其中旋轉點默認為圖片左上角是(0,0)。

現在寫一個rotate動畫的XML文件:rotate_anim.xml

[java] view plain copy
  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"//設置轉動速率,這裏設置的是勻速轉動
  6. android:pivotX="50%"
  7. android:pivotY="50%"
  8. android:toDegrees="359"
  9. android:duration = "1500"
  10. android:repeatCount = "-1"
  11. android:drawable = "@drawable/ic_launcher"
  12. android:visible = "true">
  13. </rotate>
  14. </rotate>

這裏需要註意下:

android:pivotX 與 android:pivotY 這兩個屬性值為float,可以給具體數也可以給百分比。比如你知道圖片大小是100,你可以給(50,50)表示旋轉中心點距圖片左邊50,右邊50

如果不知道圖片的準確大小,可以跟代碼所示,給百分比。

上面的代碼中還有一些在官方API中並未提及的屬性,但是這些屬性依舊可以使用到。


android:interpolator:這個屬性是用來設置轉動速率的。

LinearInterpolator為勻速效果,Accelerateinterpolator為加速效果、DecelerateInterpolator為減速效果,

android:repeatCount 重復的次數,默認為0,必須是int,可以為-1表示不停止

android:duration屬性表示從android:fromDegrees轉動到android:toDegrees所花費的時間,單位為毫秒。可以用來計算速度。

android:startOffset 在調用start函數之後等待開始運行的時間,單位為毫秒,若為10,表示10ms後開始運行

android:repeatMode 重復的模式,默認為restart,即重頭開始重新運行,可以為reverse即從結束開始向前重新運行。

在android:repeatCount大於0或為infinite時生效

android:detachWallpaper 表示是否在壁紙上運行

android:zAdjustment 表示被animated的內容在運行時在z軸上的位置,默認為normal。normal保持內容當前的z軸順序top運行時在最頂層顯示bottom運行時在最底層顯示


以上屬性中,博主親測,均可以正常使用。

布局文件 activity_main.xml,沒什麽特別的:

[java] view plain copy
  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 plain copy
  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. }



這樣,運行工程,一個旋轉的Android小綠人就出現了。PS:不知道怎麽制作動態圖,效果展示不了。

在編寫過程中有兩個地方需要大家註意:

1、在rotate_anim.xml文件中,最外層的item名字要為rotate,而不是set

[java] view plain copy
  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"//設置轉動速率,這裏設置的是勻速轉動
  6. android:pivotX="50%"
  7. android:pivotY="50%"
  8. android:toDegrees="359"
  9. android:duration = "1500"
  10. android:repeatCount = "-1"
  11. android:drawable = "@drawable/ic_launcher"
  12. android:visible = "true">
  13. </rotate>
  14. </set>

如果代碼如上所示,是set的話,屬性android:interpolator就會失效。具體原因我沒有找到,有知道的朋友希望能在博文下方解惑,多謝。

這種情況下 需要在代碼中設置動畫的interpolator:

[java] view plain copy
  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.我試過在布局文件中直接設置ImageView的src:android:src="@drawable/rotate_anim" 結果是圖片會出現,但是不會旋轉,所以不要這樣做。

3. 如果ImageView本身就帶圖片了,那麽rotate裏面設置的drawable屬性是無效的。會優先使用ImageView自身的圖片


4.設置android:drawable屬性的時候,不要忘了設置android:visible = "true",因為它默認是false(不可見)的。

在Java代碼中:

RotateAnimation共有三個構造方法,這裏詳細講述第三個,也就是參數最多的那個。 下面是Google官網中對於RotateAnimation的相關參數介紹: 技術分享

這裏面大部分參數已經在上面介紹過了,重點說下pivotXType與pivotYType

int pivotXType, 動畫在X軸相對於物件位置類型,與下面的pivotXValue結合,確定X軸上旋轉中心。

可能值為:Animation.ABSOLUTE,Animation.RELATIVE_TO_SELF, Animation.RELATIVE_TO_PARENT

如果pivotXType=Animation.ABSOLUTE,則此參數為旋轉中心在屏幕上X軸的值;

如果pivotXType=Animation.RELATIVE_TO_PARENT,則參數pivotXValue為旋轉中心在父控件水平位置百分比,如0.5表示在父控件水平方向中間位置;
如果pivotXType=Animation.RELATIVE_TO_SELF,則參數pivotXValue為旋轉中心在控件自身水平位置百分比,如果X和Y的Value都設置為0.5,則該控件以自身中心旋轉。

好了,貼代碼: activity_main.xml

[java] view plain copy
  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 plain copy
  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. //用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. //用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(1500);//設置動畫持續時間
  27. rotate.setRepeatCount(-1);//設置重復次數
  28. rotate.setFillAfter(true);//動畫執行完後是否停留在執行完的狀態
  29. rotate.setStartOffset(10);//執行前的等待時間
  30. img.setAnimation(rotate);
  31. }
  32. }

rotate裏還有很多set屬性的方法,跟XML中的屬性一一對應,有興趣的可以自己去嘗試一下。

原創博文,轉載請註明出處

以上就是RotateAnimation的內容,有不對的地方歡迎指正~

點擊下載源碼

參考文章:

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

Android 常用動畫之RotateAnimation