Android Tween动画之RotateAnimation实现图片不停旋转效果实例介绍

时间:2021-10-11 00:51:28 

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

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

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


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/infoOperating"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/operating"
android:scaleType="center">
</ImageView>
</LinearLayout>


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


ImageView infoOperatingIV = (ImageView)findViewById(R.id.infoOperating);


2、定义rotate旋转效果
在res/anim文件夹下新建tip.xml文件,内容如下


<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<rotate
android:fromDegrees="0"
android:toDegrees="359"
android:duration="500"
android:repeatCount="-1"
android:pivotX="50%"
android:pivotY="50%" />
</set>


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

java代码为


Animation operatingAnim = AnimationUtils.loadAnimation(this, R.anim.tip);
LinearInterpolator lin = new LinearInterpolator();
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. 循环运行


android:fromDegrees="0"
android:toDegrees="360"
android:repeatCount="-1"


android:repeatCount="-1"即表示循环运行,配合上android:fromDegrees="0" android:toDegrees="360"表示不间断
3、开始和停止旋转
在操作开始之前调用


if (operatingAnim != null) {
infoOperatingIV.startAnimation(operatingAnim);
}


在操作完成时调用


infoOperatingIV.clearAnimation();


许多朋友不知道如何停止旋转animation,所以强制设置rotate转动多少圈表示操作,但却无法与操作实际的进度匹配上,实际上只要如上代码所示清除animation即可。
其他:
对于上面的转动在横屏(被设置为了不重绘activity)时会出现问题,即旋转中心偏移,导致动画旋转偏离原旋转中心。解决如下


@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if (operatingAnim != null && infoOperatingIV != null && operatingAnim.hasStarted()) {
infoOperatingIV.clearAnimation();
infoOperatingIV.startAnimation(operatingAnim);
}
}
标签:Tween,RotateAnimation,旋转
0
投稿

猜你喜欢

  • Input系统之InputReader处理触摸事件案例

    2022-12-18 07:32:35
  • C#通过反射打开相应窗体方法分享

    2023-09-21 07:37:14
  • C#简单的向量用法实例教程

    2022-09-27 09:57:29
  • springboot无法从静态上下文中引用非静态变量的解决方法

    2022-03-05 16:50:05
  • arthas排查jvm中CPU占用过高问题解决

    2022-07-15 20:31:49
  • java实现短地址服务的方法(附代码)

    2023-11-15 19:13:41
  • JavaCV实现读取视频信息及自动截取封面图详解

    2022-05-20 14:43:05
  • c# 实现康威生命游戏(细胞自动机)的示例

    2022-01-08 04:36:22
  • SpringBoot中使用Session共享实现分布式部署的示例代码

    2022-10-17 04:27:54
  • Unity实现打砖块游戏

    2023-12-06 04:45:04
  • Android手机通过rtp发送aac数据给vlc播放的实现步骤

    2023-11-10 00:48:02
  • 安卓模拟器genymotion的安装与使用图文教程

    2021-12-13 19:28:36
  • Android禁止EditText自动弹出软键盘的方法及遇到问题

    2021-07-18 06:35:55
  • 模仿美团点评的Android应用中价格和购买栏悬浮固定的效果

    2021-11-12 12:00:42
  • Jmeter测试必知的名词及环境搭建

    2022-11-23 19:34:07
  • Java线程编程中isAlive()和join()的使用详解

    2023-10-29 18:08:07
  • Android仿微信语音消息的录制和播放功能

    2022-08-15 09:28:32
  • C#实现简单的RSA非对称加密算法示例

    2022-07-09 18:16:37
  • flutter实现发送验证码功能

    2023-07-05 19:03:12
  • Automapper实现自动映射的实例代码

    2023-08-14 16:37:10
  • asp之家 软件编程 m.aspxhome.com