本文主要介紹Android中如何使用rotate實現圖片不停旋轉的效果。Android 平臺提供了兩類動畫,一類是 Tween 動畫,即通過對場景里的對象不斷做圖像變換(平移、縮放、旋轉)產生動畫效果;第二類是 Frame 動畫,即順序播放事先做好的圖像,跟電影類似。
1、定義一個ImageView
定義一個ImageView是為了裝載圖片,其中的圖片將被rotate用來進行旋轉,其他View亦可。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/img"
android:layout_centerInParent="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/air_condition"/>
</RelativeLayout>
其中的android:src
為圖片內容,可使用附件中的圖片。
2、定義rotate旋轉效果
在res/anim文件夾下新建rotate_anim.xml文件,內容如下
<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android">
<rotate
android:drawable="@drawable/air_condition"
android:fromDegrees="0"
android:toDegrees="359"
android:pivotX="50%"
android:pivotY="50%"
android:duration="1000"
android:repeatCount="-1">
</rotate>
</rotate>
含義表示從0到359度開始循環旋轉,0-359(若設置成360在停止時會出現停頓現象)度旋轉所用時間為500ms,旋轉中心距離view的左頂點為50%距離,距離view的上邊緣為50%距離,即正中心,具體每個含義見下面的具體屬性介紹。
Animation operatingAnim = AnimationUtils.loadAnimation(this, R.anim.rotate_anim);
LinearInterpolator lin = new LinearInterpolator();
operatingAnim.setInterpolator(lin);
setInterpolator表示設置旋轉速率。LinearInterpolator為勻速效果,Accelerateinterpolator為加速效果、DecelerateInterpolator為減速效果,具體可見下面android:interpolator的介紹。
a. 關于其中的屬性意義如下:
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運行時在最底層顯示
b. 運行速度
運行速度為運行時間(android:duration)除以運行角度差(android:toDegrees-android:fromDegrees),比如android:duration為1000,android:toDegrees為360,android:fromDegrees為0就表示1秒轉1圈。
c. 循環運行
android:fromDegrees="0"
android:toDegrees="360"
android:repeatCount="-1"
android:repeatCount="-1"即表示循環運行,配合上android:fromDegrees="0" android:toDegrees="360"表示不間斷
3、開始和停止旋轉
在操作開始之前調用
Animation rotate = AnimationUtils.loadAnimation(this, R.anim.rotate_anim);
if (rotate != null) {
img.startAnimation(rotate);
} else {
img.setAnimation(rotate);
img.startAnimation(rotate);
}
在操作完成時調用
img.clearAnimation();
許多朋友不知道如何停止旋轉animation,所以強制設置rotate轉動多少圈表示操作,但卻無法與操作實際的進度匹配上,實際上只要如上代碼所示清除animation即可。
對于上面的轉動在橫屏(被設置為了不重繪activity)時會出現問題,即旋轉中心偏移,導致動畫旋轉偏離原旋轉中心。解決如下
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if (operatingAnim != null && infoOperatingIV != null && operatingAnim.hasStarted()) {
infoOperatingIV.clearAnimation();
infoOperatingIV.startAnimation(operatingAnim);
}
}
4用Java codes同樣也能實現相同的效果
RotateAnimation rotate = new RotateAnimation(0f, 360f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
LinearInterpolator lin = new LinearInterpolator();
rotate.setInterpolator(lin);
rotate.setDuration(2000);//設置動畫持續周期
rotate.setRepeatCount(-1);//設置重復次數
rotate.setFillAfter(true);//動畫執行完后是否停留在執行完的狀態
rotate.setStartOffset(10);//執行前的等待時間
img.setAnimation(rotate);