Android Tween動畫之RotateAnimation實現圖片不停旋轉

來源:互聯網
上載者:User

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

在新浪微部落格戶端中各個操作進行中時activity的右上方都會有個不停旋轉的表徵圖,類似重新整理的效果,給使用者以操作中的提示。這種非模態的提示方式推薦使用,那麼下面就分享下如何?這種效果吧

1、定義一個ImageView

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

資源檔為

<?xml version="1.0" encoding="utf-8"?><LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent">   <ImageViewandroid:id="@+id/infoOperating"android:layout_width="wrap_content"android:layout_height="wrap_content"android:src="@drawable/operating"android:scaleType="center"></ImageView></LinearLayout>

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

java代碼為

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

2、定義rotate旋轉效果

在res/anim檔案夾下建立tip.xml檔案,內容如下

<?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度開始迴圈旋轉,0-359(若設定成360在停止時會出現停頓現象)度旋轉所用時間為500ms,旋轉中心距離view的左頂點為50%距離,距離view的上邊緣為50%距離,即正中心,具體每個含義見下面的具體屬性介紹。

java代碼為

Animation operatingAnim = AnimationUtils.loadAnimation(this, R.anim.tip);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. 迴圈運行

Java代碼
android:fromDegrees="0"android:toDegrees="360"android:repeatCount="-1"

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

 

3、開始和停止旋轉

在操作開始之前調用

Java代碼
if (operatingAnim != null) {infoOperatingIV.startAnimation(operatingAnim);}

在操作完成時調用

Java代碼
infoOperatingIV.clearAnimation();

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

其他:

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

Java代碼
@Overridepublic void onConfigurationChanged(Configuration newConfig) {super.onConfigurationChanged(newConfig);if (operatingAnim != null && infoOperatingIV != null && operatingAnim.hasStarted()) {infoOperatingIV.clearAnimation();infoOperatingIV.startAnimation(operatingAnim);}}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.