android動畫Rotate

項目有一個需求,有一個刷新按鈕,上面放着一個常見的靜止的刷新圓圈,以下圖: html

 

 

一旦用戶按了刷新按鈕,須要讓這個刷新圓圈轉動起來,讓用戶感受到程序還在運行着,而不是卡死了。 java

 

有兩個思路,一是將這個圖按照旋轉時間不一樣旋轉成不一樣旋轉角度的圖片,就像要作一張gif圖片同樣,例如我要每次旋轉30度,就須要360\30=12張圖片,而後再anim文件夾下新建xml文件,內容以下: android

 

Xml代碼 複製代碼  收藏代碼
  1. <animation-list xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     android:oneshot="true">  
  3.     <item android:drawable="@drawable/rocket_thrust1" android:duration="200" />  
  4.     <item android:drawable="@drawable/rocket_thrust2" android:duration="200" />  
  5.     <item android:drawable="@drawable/rocket_thrust3" android:duration="200" />  
  6. </animation-list>  
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
    android:oneshot="true">
    <item android:drawable="@drawable/rocket_thrust1" android:duration="200" />
    <item android:drawable="@drawable/rocket_thrust2" android:duration="200" />
    <item android:drawable="@drawable/rocket_thrust3" android:duration="200" />
</animation-list>

 

 

在代碼中這樣寫: app

 

Java代碼 複製代碼  收藏代碼
  1. AnimationDrawable rocketAnimation;   
  2.   
  3. public void onCreate(Bundle savedInstanceState) {   
  4.   super.onCreate(savedInstanceState);   
  5.   setContentView(R.layout.main);   
  6.   
  7.   ImageView rocketImage = (ImageView) findViewById(R.id.rocket_image);   
  8.   rocketImage.setBackgroundResource(R.anim.rocket_thrust);   
  9.   rocketAnimation = (AnimationDrawable) rocketImage.getBackground();   
  10. }   
  11.   
  12. public boolean onTouchEvent(MotionEvent event) {   
  13.   if (event.getAction() == MotionEvent.ACTION_DOWN) {   
  14.     rocketAnimation.start();   
  15.     return true;   
  16.   }   
  17.   return super.onTouchEvent(event);   
  18. }  
AnimationDrawable rocketAnimation;

public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);

  ImageView rocketImage = (ImageView) findViewById(R.id.rocket_image);
  rocketImage.setBackgroundResource(R.anim.rocket_thrust);
  rocketAnimation = (AnimationDrawable) rocketImage.getBackground();
}

public boolean onTouchEvent(MotionEvent event) {
  if (event.getAction() == MotionEvent.ACTION_DOWN) {
    rocketAnimation.start();
    return true;
  }
  return super.onTouchEvent(event);
}

 

具體代碼含義參考:http://www.cnblogs.com/feisky/archive/2010/01/11/1644482.html ide

 

 

這種作法其實就是將每一幀圖片都顯示了一次,可是因爲須要更多圖片,文件體積會上升。 佈局

 

因而想到用rotate作單幀圖片旋轉,查到的資料:http://rainbowsu.iteye.com/blog/766608 ui

 

可是做者沒能實現循環旋轉,我嘗試了下,修改了下anim文件的格式,成功了 this

 

Xml代碼 複製代碼  收藏代碼
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <rotate xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android :anim/linear_interpolator"  
  3.     android:fromDegrees="0" android:toDegrees="+360" android:duration="1000"  
  4.     android:pivotX="50%" android:pivotY="50%" android:repeatCount="infinite" />  
<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/linear_interpolator"
	android:fromDegrees="0" android:toDegrees="+360" android:duration="1000"
	android:pivotX="50%" android:pivotY="50%" android:repeatCount="infinite" />

 

 

其中android:duration="1000"表示旋轉速率是1秒鐘。 spa

 

代碼: .net

 

Java代碼 複製代碼  收藏代碼
  1. package info.wegosoft;   
  2.   
  3. import android.app.Activity;   
  4. import android.os.Bundle;   
  5. import android.view.animation.Animation;   
  6. import android.view.animation.AnimationUtils;   
  7.   
  8. public class LoadingAnimationTest extends Activity {   
  9.     /** Called when the activity is first created. */  
  10.     @Override  
  11.     public void onCreate(Bundle savedInstanceState) {   
  12.         super.onCreate(savedInstanceState);   
  13.         setContentView(R.layout.main);   
  14.            
  15.         Animation anim = AnimationUtils.loadAnimation(this, R.anim.round_loading);       
  16.            
  17.         findViewById(R.id.loadingBtn).startAnimation(anim);      
  18.     }   
  19. }  
package info.wegosoft;

import android.app.Activity;
import android.os.Bundle;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;

public class LoadingAnimationTest extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        Animation anim = AnimationUtils.loadAnimation(this, R.anim.round_loading);    
        
        findViewById(R.id.loadingBtn).startAnimation(anim);   
    }
}

 

佈局文件:

 

Java代碼 複製代碼  收藏代碼
  1. <?xml version="1.0" encoding="utf-8"?>   
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical" android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent">   
  5.     <Button android:id="@+id/loadingBtn" android:layout_width="wrap_content"  
  6.         android:layout_height="wrap_content" android:background="@drawable/refresh_normal"></Button>   
  7. </LinearLayout>  
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:orientation="vertical" android:layout_width="fill_parent"
	android:layout_height="fill_parent">
	<Button android:id="@+id/loadingBtn" android:layout_width="wrap_content"
		android:layout_height="wrap_content" android:background="@drawable/refresh_normal"></Button>
</LinearLayout>

 

 

工程見附件。

 

最後提供官方文檔相關說明的連接:http://developer.android.com/guide/topics/resources/animation-resource.html

 

注意其中的勻速插值器LinearInterpolator彷佛不能設置速率,我在這浪費了不少時間。

相關文章
相關標籤/搜索