Android动画----Rotate

来源:互联网 发布:设置淘宝客推广优惠券 编辑:程序博客网 时间:2024/06/15 12:43
  • 引言

      学习了Scale、Aplha,接下来趁热打铁,这篇就学习Rotate动画吧
      

  • Rotate动画

      旋转动画,继承自基类Animaton
      

  • Rotate属性

      老规矩吧,直接代码里注释

<?xml version="1.0" encoding="utf-8"?> <rotate xmlns:android="http://schemas.android.com/apk/res/android"    android:fromDegrees="0"            #初始角度    android:toDegrees="540"            #结束时角度,值为正时顺时针旋转,值为负时逆时针旋转    android:pivotX="50%"               #旋转中心x轴坐标,取值可以是数值(50)、百分数(50%)、百                                        分数p(50%p),当取值为数值时,缩放起点为View左上角坐标                                        加具体数值像素,当取值为百分数时,表示在当前View左上角坐                                        加上View宽度的具体百分比,当取值为百分数p时,表示在View                                        左上角坐标加上父控件宽度的具体百分比    android:pivotY="50%"               #同上    android:duration="700"             #动画持续时间,毫秒为单位    android:fillAfter="true"           #动画结束后,保持结束时的状态    android:fillBefore="true"          #动画结束后,恢复为初始状态    android:fillEnabled="true"         #效果同上    android:repeatCount="5"            #重复次数,取值为-1时无限重复,默认动画执行一次    android:repeatMode ="reverse"      #重复模式,有reverse和restart两个值,前者为倒序回放,后者为重新开始    android:interpolator="@android:anim/accelerate_decelerate_interpolator" #插值器,后面单独讲                 />
  • Rotate xml使用方式

      xml的使用方式是比较普遍的,而且代码复用性也比较好,推荐使用,使用步骤基本如下:
      第一步,res目录下新建anim文件夹,在anim目录下新建rotate.xml文件(名字随意),目录结构如下:
      这里写图片描述
      第二步,编写rotate.xml的内容,示例如下

<?xml version="1.0" encoding="utf-8"?> <rotate xmlns:android="http://schemas.android.com/apk/res/android"    android:fromDegrees="0"    android:toDegrees="540"    android:duration="2000"    android:fillBefore="true"    android:repeatCount="4"    android:repeatMode="reverse"    android:interpolator="@android:anim/cycle_interpolator"    />

  第三步,实例化Rotate动画,代码如下:
  

 private Animation rotateAnimation; …… rotateAnimation = AnimationUtils.loadAnimation(AnimDemoActivity.this,R.anim.rotate);

  第四部,执行动画
  

view.startAnimation(rotateAnimation);
  • Rotate 代码使用方式

     第一步,创建Rotate动画实例,代码如下:
     

private RotateAnimation mRotateAnimation;……mRotateAnimation = new RotateAnimation(0,90,0.5f,0.5f);

  第二步,设置动画相关属性
  

mRotateAnimation.setDuration(2000);mRotateAnimation.setRepeatCount(4);mRotateAnimation.setRepeatMode(Animation.REVERSE);mRotateAnimation.setInterpolator(this,android.R.anim.accelerate_interpolator);

  第三步,执行动画
  

view.startAnimation(mRotateAnimation);
  • 动画效果
      
      动画效果基于xml方式实现,效果如下:
      这里写图片描述
      
  • 结尾

      关于Rotate动画的简单介绍,本篇博客就介绍到这里,欢迎大家指正。Demo下载点击这里

2 0
原创粉丝点击