android imageview圍繞中心旋轉動畫

本文主要介紹Android中如何使用rotate實現圖片不停旋轉的效果。Android 平臺提供了兩類動畫,一類是 Tween 動畫,即通過對場景裏的對象不斷做圖像變換(平移、縮放、旋轉)產生動畫效果;第二類是 Frame 動畫,即順序播放事先做好的圖像,跟電影類似。本文分析 Tween動畫的rotate實現旋轉效果。

 

在新浪微博客戶端中各個操作進行中時activity的右上角都會有個不停旋轉的圖標,類似刷新的效果,給用戶以操作中的提示。這種非模態的提示方式推薦使用,那麼下面就分享下如何實現這種效果吧

 

1、定義一個ImageView

定義一個ImageView是爲了裝載圖片,其中的圖片將被rotate用來進行旋轉,其他View亦可。

資源文件爲

Java代碼   收藏代碼

  1. <?xml version="1.0" encoding="utf-8"?>  

  2. <LinearLayout  

  3.     xmlns:android="http://schemas.android.com/apk/res/android"  

  4.     android:layout_width="match_parent"  

  5.     android:layout_height="match_parent">  

  6.     <ImageView  

  7.         android:id="@+id/infoOperating"  

  8.         android:layout_width="wrap_content"  

  9.         android:layout_height="wrap_content"  

  10.         android:src="@drawable/operating"  

  11.         android:scaleType="center">  

  12.         </ImageView>  

  13. </LinearLayout>  

其中的android:src爲圖片內容,可使用附件中的圖片。

java代碼爲

Java代碼   收藏代碼

  1. ImageView infoOperatingIV = (ImageView)findViewById(R.id.infoOperating);  

 

2、定義rotate旋轉效果

在res/anim文件夾下新建tip.xml文件,內容如下

Java代碼   收藏代碼

  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:toDegrees="359"  

  6.         android:duration="500"  

  7.         android:repeatCount="-1"  

  8.         android:pivotX="50%"  

  9.         android:pivotY="50%" />  

  10. </set>  

含義表示從0到359度開始循環旋轉,0-359(若設置成360在停止時會出現停頓現象)度旋轉所用時間爲500ms,旋轉中心距離view的左頂點爲50%距離,距離view的上邊緣爲50%距離,即正中心,具體每個含義見下面的具體屬性介紹。

java代碼爲

Java代碼   收藏代碼

  1. Animation operatingAnim = AnimationUtils.loadAnimation(this, R.anim.tip);  

  2. LinearInterpolator lin = new LinearInterpolator();  

  3. 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. 循環運行

Java代碼   收藏代碼

  1. android:fromDegrees="0"  

  2. android:toDegrees="360"  

  3. android:repeatCount="-1"  

android:repeatCount="-1"即表示循環運行,配合上android:fromDegrees="0" android:toDegrees="360"表示不間斷

 

3、開始和停止旋轉

在操作開始之前調用

Java代碼   收藏代碼

  1. if (operatingAnim != null) {  

  2.     infoOperatingIV.startAnimation(operatingAnim);  

  3. }  

在操作完成時調用

Java代碼   收藏代碼

  1. infoOperatingIV.clearAnimation();  

許多朋友不知道如何停止旋轉animation,所以強制設置rotate轉動多少圈表示操作,但卻無法與操作實際的進度匹配上,實際上只要如上代碼所示清除animation即可。

 

 

其他:

對於上面的轉動在橫屏(被設置爲了不重繪activity)時會出現問題,即旋轉中心偏移,導致動畫旋轉偏離原旋轉中心。解決如下

Java代碼   收藏代碼

  1. @Override  

  2. public void onConfigurationChanged(Configuration newConfig) {  

  3.   

  4.     super.onConfigurationChanged(newConfig);  

  5.   

  6.     if (operatingAnim != null && infoOperatingIV != null && operatingAnim.hasStarted()) {  

  7.         infoOperatingIV.clearAnimation();  

  8.         infoOperatingIV.startAnimation(operatingAnim);  

  9.     }  


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章