【转】Android单帧动画Rotate旋转

时间:2025-02-03 18:04:38

项目有一个需求,有一个刷新按钮,上面放着一个常见的静止的刷新圆圈,如下图:

【转】Android单帧动画Rotate旋转

一旦用户按了刷新按钮,需要让这个刷新圆圈转动起来,让用户感觉到程序还在运行着,而不是卡死了。

有两个思路,一是将这个图按照旋转时间不同旋转成不同旋转角度的图片,就像要做一张gif图片一样,例如我要每次旋转30度,就需要360\30=12张图片,然后再anim文件夹下新建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>

在代码中这样写:

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

具体代码含义参考:http://www.cnblogs.com/feisky/archive/2010/01/11/1644482.html

这种做法其实就是将每一帧图片都显示了一次,但是由于需要更多图片,文件体积会上升。

于是想到用rotate做单帧图片旋转,查到的资料:http://rainbowsu.iteye.com/blog/766608

但是作者没能实现循环旋转,我尝试了下,修改了下anim文件的格式,成功了

  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" />

其中Android:duration="1000"表示旋转速率是1秒钟。

代码:

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

布局文件:

  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>

工程见附件。

最后提供官方文档相关说明的链接:http://developer.android.com/guide/topics/resources/animation-resource.html

注意其中的匀速插值器LinearInterpolator似乎不能设置速率,我在这浪费了很多时间。