当前位置: 代码迷 >> Android >> Android单帧卡通片Rotate旋转
  详细解决方案

Android单帧卡通片Rotate旋转

热度:66   发布时间:2016-05-01 18:46:53.0
Android单帧动画Rotate旋转

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

?

?

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

?

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

?

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

?

?

在代码中这样写:

?

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

?

?

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

?

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

?

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

?

<?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秒钟。

?

代码:

?

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);       }}

?

布局文件:

?

<?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似乎不能设置速率,我在这浪费了很多时间。

?

?

1 楼 york144122 2011-10-15  
貌似把 android:toDegrees="+360" 改成 android:toDegrees="+359" 就完美了。哈哈,感觉是。。。流畅多了
2 楼 furlxy 2011-12-19  
不错 
  相关解决方案