主要介紹Android中如何使用rotate實(shí)現(xiàn)圖片不停旋轉(zhuǎn)的效果。Android 平臺(tái)提供了兩類動(dòng)畫,一類是 Tween 動(dòng)畫,即通過對(duì)場(chǎng)景里的對(duì)象不斷做圖像變換(平移、縮放、旋轉(zhuǎn))產(chǎn)生動(dòng)畫效果;第二類是 Frame 動(dòng)畫,即順序播放事先做好的圖像,跟電影類似。本文分析 Tween動(dòng)畫的rotate實(shí)現(xiàn)旋轉(zhuǎn)效果。
在新浪微博客戶端中各個(gè)操作進(jìn)行中時(shí)activity的右上角都會(huì)有個(gè)不停旋轉(zhuǎn)的圖標(biāo),類似刷新的效果,給用戶以操作中的提示。這種非模態(tài)的提示方式推薦使用,那么下面就分享下如何實(shí)現(xiàn)這種效果吧
1、定義一個(gè)ImageView
定義一個(gè)ImageView是為了裝載圖片,其中的圖片將被rotate用來進(jìn)行旋轉(zhuǎn),其他View亦可。
資源文件為
復(fù)制代碼 代碼如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/infoOperating"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/operating"
android:scaleType="center">
</ImageView>
</LinearLayout>
其中的android:src為圖片內(nèi)容,可使用附件中的圖片。
java代碼為
復(fù)制代碼 代碼如下:
ImageView infoOperatingIV = (ImageView)findViewById(R.id.infoOperating);
2、定義rotate旋轉(zhuǎn)效果
在res/anim文件夾下新建tip.xml文件,內(nèi)容如下
復(fù)制代碼 代碼如下:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<rotate
android:fromDegrees="0"
android:toDegrees="359"
android:duration="500"
android:repeatCount="-1"
android:pivotX="50%"
android:pivotY="50%" />
</set>
含義表示從0到359度開始循環(huán)旋轉(zhuǎn),0-359(若設(shè)置成360在停止時(shí)會(huì)出現(xiàn)停頓現(xiàn)象)度旋轉(zhuǎn)所用時(shí)間為500ms,旋轉(zhuǎn)中心距離view的左頂點(diǎn)為50%距離,距離view的上邊緣為50%距離,即正中心,具體每個(gè)含義見下面的具體屬性介紹。
java代碼為
復(fù)制代碼 代碼如下:
Animation operatingAnim = AnimationUtils.loadAnimation(this, R.anim.tip);
LinearInterpolator lin = new LinearInterpolator();
operatingAnim.setInterpolator(lin);
setInterpolator表示設(shè)置旋轉(zhuǎn)速率。LinearInterpolator為勻速效果,Accelerateinterpolator為加速效果、DecelerateInterpolator為減速效果,具體可見下面android:interpolator的介紹。
a. 關(guān)于其中的屬性意義如下(紅色部分加以注意):
android:fromDegrees 起始的角度度數(shù)
android:toDegrees 結(jié)束的角度度數(shù),負(fù)數(shù)表示逆時(shí)針,正數(shù)表示順時(shí)針。如10圈則比android:fromDegrees大3600即可
android:pivotX 旋轉(zhuǎn)中心的X坐標(biāo)
浮點(diǎn)數(shù)或是百分比。浮點(diǎn)數(shù)表示相對(duì)于Object的左邊緣,如5; 百分比表示相對(duì)于Object的左邊緣,如5%; 另一種百分比表示相對(duì)于父容器的左邊緣,如5%p; 一般設(shè)置為50%表示在Object中心
android:pivotY 旋轉(zhuǎn)中心的Y坐標(biāo)
浮點(diǎn)數(shù)或是百分比。浮點(diǎn)數(shù)表示相對(duì)于Object的上邊緣,如5; 百分比表示相對(duì)于Object的上邊緣,如5%; 另一種百分比表示相對(duì)于父容器的上邊緣,如5%p; 一般設(shè)置為50%表示在Object中心
android:duration 表示從android:fromDegrees轉(zhuǎn)動(dòng)到android:toDegrees所花費(fèi)的時(shí)間,單位為毫秒。可以用來計(jì)算速度。
android:interpolator表示變化率,但不是運(yùn)行速度。一個(gè)插補(bǔ)屬性,可以將動(dòng)畫效果設(shè)置為加速,減速,反復(fù),反彈等。默認(rèn)為開始和結(jié)束慢中間快,
android:startOffset 在調(diào)用start函數(shù)之后等待開始運(yùn)行的時(shí)間,單位為毫秒,若為10,表示10ms后開始運(yùn)行
android:repeatCount 重復(fù)的次數(shù),默認(rèn)為0,必須是int,可以為-1表示不停止
android:repeatMode 重復(fù)的模式,默認(rèn)為restart,即重頭開始重新運(yùn)行,可以為reverse即從結(jié)束開始向前重新運(yùn)行。在android:repeatCount大于0或?yàn)閕nfinite時(shí)生效
android:detachWallpaper 表示是否在壁紙上運(yùn)行
android:zAdjustment 表示被animated的內(nèi)容在運(yùn)行時(shí)在z軸上的位置,默認(rèn)為normal。
normal保持內(nèi)容當(dāng)前的z軸順序
top運(yùn)行時(shí)在最頂層顯示
bottom運(yùn)行時(shí)在最底層顯示
b. 運(yùn)行速度
運(yùn)行速度為運(yùn)行時(shí)間(android:duration)除以運(yùn)行角度差(android:toDegrees-android:fromDegrees),比如android:duration為1000,android:toDegrees為360,android:fromDegrees為0就表示1秒轉(zhuǎn)1圈。
c. 循環(huán)運(yùn)行
復(fù)制代碼 代碼如下:
android:fromDegrees="0"
android:toDegrees="360"
android:repeatCount="-1"
android:repeatCount="-1"即表示循環(huán)運(yùn)行,配合上android:fromDegrees="0" android:toDegrees="360"表示不間斷
3、開始和停止旋轉(zhuǎn)
在操作開始之前調(diào)用
復(fù)制代碼 代碼如下:
if (operatingAnim != null) {
infoOperatingIV.startAnimation(operatingAnim);
}
在操作完成時(shí)調(diào)用
復(fù)制代碼 代碼如下:
infoOperatingIV.clearAnimation();
許多朋友不知道如何停止旋轉(zhuǎn)animation,所以強(qiáng)制設(shè)置rotate轉(zhuǎn)動(dòng)多少圈表示操作,但卻無法與操作實(shí)際的進(jìn)度匹配上,實(shí)際上只要如上代碼所示清除animation即可。
其他:
對(duì)于上面的轉(zhuǎn)動(dòng)在橫屏(被設(shè)置為了不重繪activity)時(shí)會(huì)出現(xiàn)問題,即旋轉(zhuǎn)中心偏移,導(dǎo)致動(dòng)畫旋轉(zhuǎn)偏離原旋轉(zhuǎn)中心。解決如下
復(fù)制代碼 代碼如下:
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if (operatingAnim != null && infoOperatingIV != null && operatingAnim.hasStarted()) {
infoOperatingIV.clearAnimation();
infoOperatingIV.startAnimation(operatingAnim);
}
}