转载:http://my.oschina.net/janson2013/blog/118558?fromerr=X2n8iSf8

本文主要介绍Android中如何使用rotate实现图片不停旋转的效果。Android 平台提供了两类动画,一类是 Tween 动画,即通过对场景里的对象不断做图像变换(平移、缩放、旋转)产生动画效果;第二类是 Frame 动画,即顺序播放事先做好的图像,跟电影类似。本文分析 Tween动画的rotate实现旋转效果。

在新浪微博客户端中各个操作进行中时activity的右上角都会有个不停旋转的图标,类似刷新的效果,给用户以操作中的提示。这种非模态的提示方式推荐使用,那么下面就分享下如何实现这种效果吧

1、定义一个ImageView

定义一个ImageView是为了装载图片,其中的图片将被rotate用来进行旋转,其他View亦可。

资源文件为

Java代码
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <LinearLayout
  3. xmlns:android="http://schemas.android.com/apk/res/android"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent">
  6. <ImageView
  7. android:id="@+id/infoOperating"
  8. android:layout_width="wrap_content"
  9. android:layout_height="wrap_content"
  10. android:src="@drawable/operating"
  11. android:scaleType="center">
  12. </ImageView>
  13. </LinearLayout>

其中的android:src为图片内容,可使用附件中的图片。

java代码为

Java代码
  1. ImageViewinfoOperatingIV=(ImageView)findViewById(R.id.infoOperating);

2、定义rotate旋转效果

在res/anim文件夹下新建tip.xml文件,内容如下

Java代码
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <setxmlns:android="http://schemas.android.com/apk/res/android">
  3. <rotate
  4. android:fromDegrees="0"
  5. android:toDegrees="359"
  6. android:duration="500"
  7. android:repeatCount="-1"
  8. android:pivotX="50%"
  9. android:pivotY="50%"/>
  10. </set>

含义表示从0到359度开始循环旋转,0-359(若设置成360在停止时会出现停顿现象)度旋转所用时间为500ms,旋转中心距离view的左顶点为50%距离,距离view的上边缘为50%距离,即正中心,具体每个含义见下面的具体属性介绍。

java代码为

Java代码
  1. AnimationoperatingAnim=AnimationUtils.loadAnimation(this,R.anim.tip);
  2. LinearInterpolatorlin=newLinearInterpolator();
  3. operatingAnim.setInterpolator(lin);

setInterpolator表示设置旋转速率。LinearInterpolator为匀速效果,Accelerateinterpolator为加速效果、DecelerateInterpolator为减速效果,具体可见下面android:interpolator的介绍。

a. 关于其中的属性意义如下(红色部分加以注意):

android:fromDegrees起始的角度度数

android:toDegrees结束的角度度数,负数表示逆时针,正数表示顺时针。如10圈则比android:fromDegrees大3600即可

android:pivotX旋转中心的X坐标

浮点数或是百分比。浮点数表示相对于Object的左边缘,如5; 百分比表示相对于Object的左边缘,如5%; 另一种百分比表示相对于父容器的左边缘,如5%p; 一般设置为50%表示在Object中心

android:pivotY旋转中心的Y坐标

浮点数或是百分比。浮点数表示相对于Object的上边缘,如5; 百分比表示相对于Object的上边缘,如5%; 另一种百分比表示相对于父容器的上边缘,如5%p; 一般设置为50%表示在Object中心

android:duration表示从android:fromDegrees转动到android:toDegrees所花费的时间,单位为毫秒。可以用来计算速度。

android:interpolator表示变化率,但不是运行速度。一个插补属性,可以将动画效果设置为加速,减速,反复,反弹等。默认为开始和结束慢中间快,

android:startOffset在调用start函数之后等待开始运行的时间,单位为毫秒,若为10,表示10ms后开始运行

android:repeatCount重复的次数,默认为0,必须是int,可以为-1表示不停止

android:repeatMode重复的模式,默认为restart,即重头开始重新运行,可以为reverse即从结束开始向前重新运行。在android:repeatCount大于0或为infinite时生效

android:detachWallpaper表示是否在壁纸上运行

android:zAdjustment表示被animated的内容在运行时在z轴上的位置,默认为normal。

normal保持内容当前的z轴顺序

top运行时在最顶层显示

bottom运行时在最底层显示

b. 运行速度

运行速度为运行时间(android:duration)除以运行角度差(android:toDegrees-android:fromDegrees),比如android:duration为1000,android:toDegrees为360,android:fromDegrees为0就表示1秒转1圈。

c. 循环运行

Java代码
  1. android:fromDegrees="0"
  2. android:toDegrees="360"
  3. android:repeatCount="-1"

android:repeatCount="-1"即表示循环运行,配合上android:fromDegrees="0" android:toDegrees="360"表示不间断

3、开始和停止旋转

在操作开始之前调用

Java代码
  1. if(operatingAnim!=null){
  2. infoOperatingIV.startAnimation(operatingAnim);
  3. }

在操作完成时调用

Java代码
  1. infoOperatingIV.clearAnimation();

许多朋友不知道如何停止旋转animation,所以强制设置rotate转动多少圈表示操作,但却无法与操作实际的进度匹配上,实际上只要如上代码所示清除animation即可。

其他:

对于上面的转动在横屏(被设置为了不重绘activity)时会出现问题,即旋转中心偏移,导致动画旋转偏离原旋转中心。解决如下

Java代码
  1. @Override
  2. publicvoidonConfigurationChanged(ConfigurationnewConfig){
  3. super.onConfigurationChanged(newConfig);
  4. if(operatingAnim!=null&&infoOperatingIV!=null&&operatingAnim.hasStarted()){
  5. infoOperatingIV.clearAnimation();
  6. infoOperatingIV.startAnimation(operatingAnim);
  7. }
  8. }

——————————————————————————————————————————————————————————————————————————

项目中: 美食地图-中间刷新运用到。 步骤一模一样

更多相关文章

  1. android studio中常用的导入jar包以及添加远程依赖的方式
  2. android widget 开发实例
  3. android开发之Google工程师多图详解Android架构
  4. Flutter——踩坑之旅(现有Android项目和现有iOS项目引入同一个Flu
  5. 使用kotlin编写Android第一个Activity
  6. Android(安卓)TabActivity Essentials
  7. Android中几种常见的定时刷新方式
  8. android lint 是什么
  9. Android:你不能忽略的代码命名规范

随机推荐

  1. Html jquery实现根据 IOS和Android访问跳
  2. Android(安卓)SQLiteDatabase中query、in
  3. Android(安卓)Window 二 可移动悬浮窗口
  4. android关于手机和3.0版本以上平板去标题
  5. aar文件的libs有其他jar文件,使用时的注意
  6. Android基于自带的DownloadManager实现下
  7. java更改android文件权限--待验证
  8. Android(安卓)应用进程启动过程
  9. 使用 Android(安卓)Studio 进行测试 (一)
  10. Android(安卓)Service和IntentService知