Android 围绕中心旋转动画

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

1、定义一个ImageView

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

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/img"
        android:layout_centerInParent="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/air_condition"/>

</RelativeLayout>

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

2、定义rotate旋转效果

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

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

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

Animation operatingAnim = AnimationUtils.loadAnimation(this, R.anim.rotate_anim);  
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、开始和停止旋转

在操作开始之前调用

Animation rotate = AnimationUtils.loadAnimation(this, R.anim.rotate_anim);
if (rotate != null) {  
    img.startAnimation(rotate);  
}  else {
    img.setAnimation(rotate);
    img.startAnimation(rotate);  
}

在操作完成时调用

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

4用Java codes同样也能实现相同的效果

RotateAnimation rotate  = new RotateAnimation(0f, 360f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        LinearInterpolator lin = new LinearInterpolator();  
        rotate.setInterpolator(lin);
        rotate.setDuration(2000);//设置动画持续周期
        rotate.setRepeatCount(-1);//设置重复次数
        rotate.setFillAfter(true);//动画执行完后是否停留在执行完的状态 
        rotate.setStartOffset(10);//执行前的等待时间 
        img.setAnimation(rotate); 
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 175,856评论 25 709
  • 最近工作比较清闲,所以想系统的复习和学习下自己比较短缺的知识,所以。。。 程序运行效果图: Android动画主要...
    小沈新手阅读 3,434评论 0 1
  • 1 背景 不能只分析源码呀,分析的同时也要整理归纳基础知识,刚好有人微博私信让全面说说Android的动画,所以今...
    未聞椛洺阅读 7,664评论 0 10
  • Animation Animation类是所有动画(scale、alpha、translate、rotate)的基...
    四月一号阅读 5,914评论 0 10
  • 我觉着,这个时代最不缺的是意外。意外是什么?就是料想之外的事件。如果你性格是豁达类型的,可以解释为惊喜-人生无处不...
    世间尘的尘阅读 2,600评论 0 0