欢迎访问 生活随笔!

生活随笔

当前位置: 首页 >

android动画Rotate

发布时间:2023/12/31 42 豆豆
生活随笔 收集整理的这篇文章主要介绍了 android动画Rotate 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

2019独角兽企业重金招聘Python工程师标准>>>

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

 

 

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

 

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

 

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

     

     

    在代码中这样写:

     

    Java代码  
  • 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);   
  • }  
  • 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代码  
  • <?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" />  
  • <?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秒钟。

     

    代码:

     

    Java代码  
  • 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);      
  •     }   
  • }  
  • 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. */@Overridepublic 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代码  
  • <?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>  
  • <?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似乎不能设置速率,我在这浪费了很多时间。

    转载于:https://my.oschina.net/u/586684/blog/170406

    总结

    以上是生活随笔为你收集整理的android动画Rotate的全部内容,希望文章能够帮你解决所遇到的问题。

    如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。