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彷佛不能设置速率,我在这浪费了不少时间。

相关文章
相关标签/搜索