Android Animation共有四大類型,分别是
Alpha 透明度動畫
Scale 大小伸縮動畫
Translate 位移動畫
Rotate 旋轉動畫
這四類動畫按模式又可分為:
tweened animation(漸變動畫) —— alpha 與 scale
frame by frame(畫面轉換動畫) —— translate 與 rotate
官方給予的Rotate常用屬性如上所示。
android:drawable 需要進行旋轉動畫的圖檔
android:fromDegrees 旋轉的起始點(旋轉開始的角度)
android:toDegrees 旋轉的結束點(旋轉最終角度)
andoird:pivotX 旋轉點的X值(距離左側的偏移量)
android:pivotY旋轉點的Y值(距離頂部的偏移量)
android: visible這個好了解,就是圖檔初始的顯示狀态
詳細屬性說明如下:
android:fromDegrees
起始的角度度數。
android:toDegrees
結束的角度度數,負數表示逆時針,正數表示順時針。如10圈則比android:fromDegrees大3600即可。
android:pivotX
旋轉中心的X坐标。浮點數或是百分比。浮點數表示相對于Object的左邊緣,如5; 百分比表示相對于Object的左邊緣,如5%; 另一種百分比表示相對于父容器的左邊緣,如5%p; 一般設定為50%表示在Object中心。
android:pivotY
旋轉中心的Y坐标。浮點數或是百分比。浮點數表示相對于Object的上邊緣,如5; 百分比表示相對于Object的上邊緣,如5%; 另一種百分比表示相對于父容器的上邊緣,如5%p; 一般設定為50%表示在Object中心
android:duration
表示從android:fromDegrees轉動到android:toDegrees所花費的時間,機關為毫秒。可以用來計算速度。
android:interpolator
表示變化率,但不是運作速度。一個插補屬性,可以将動畫效果設定為加速,減速,反複,反彈等。預設為開始和結束慢中間快,
android:startOffset
在調用start函數之後等待開始運作的時間,機關為毫秒,若為10,表示10ms後開始運作。
android:repeatCount
重複的次數,預設為0,必須是int,可以為-1表示不停止。
android:repeatMode
重複的模式預設為restart即重頭開始重新運作,可以為reverse即從結束開始向前重新運作。在android:repeatCount大于0或為infinite時生效。
android:detachWallpaper
表示是否在桌面上運作。
android:zAdjustment
表示被animated的内容在運作時在z軸上的位置,預設為normal。normal保持内容目前的z軸順序,top運作時在最頂層顯示,bottom運作時在最底層顯示。
在res目錄下建立anim檔案夾,并建立動畫rotate_anim.xml檔案:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<rotate
android:fromDegrees="0"
android:toDegrees="360"
android:duration="900"
android:repeatCount="-1"
android:pivotX="50%"
android:pivotY="50%"/>
</set>
在activity_main.xml主布局檔案中添加imageView布局控件
<ImageView
android:id="@+id/ivRotatePicture1"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_centerInParent="true"
android:src="@drawable/pic_wind" />
最後看下main_activity.java對Animation的實作:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = (ImageView) findViewById(R.id.ivRotatePicture);
imageView.setOnClickListener(this);//這裡想點選圖檔 改變旋轉速度
animation = AnimationUtils.loadAnimation(this, R.anim.rotate_anim);
LinearInterpolator lin = new LinearInterpolator();//設定旋轉方式為勻速旋轉
animation.setInterpolator(lin);//設定旋轉方式為勻速旋轉
animation.setDuration(1200);//設定旋轉一周時間
imageView.setAnimation(animation);//将動畫和image控件相關聯
imageView.startAnimation(animation);//最後啟動動畫
}
付上運作效果圖
最後附上自己調試代碼源碼,歡迎提意見!
http://download.csdn.net/detail/u014159143/9911289