安卓AnimationDrawable之旋转动画

    在安卓开发中经常会看到旋转动画,这种动画效果有两种实现方式,一种是基于java代码,一种是在xml文件中定义,然后引用xml文件

  纯java代码实现:

1      Animation animation = new RotateAnimation(0, 359);  
2         animation.setDuration(500);  
3         animation.setRepeatCount(8);//动画的重复次数  
4         animation.setFillAfter(true);//设置为true,动画转化结束后被应用  
5         imageView1.startAnimation(animation);//开始动画

  通过配置文件实现:

    在res文件下创建一个新的文件夹anim,这里的文件夹名字必须是anim,然后在anim中新建一个xml文件rotate(自定义),在这个xml文件中定义动画属性

 1 <set xmlns:android="http://schemas.android.com/apk/res/android">
 2 
 3     <rotate
 4         android:duration="5000"//旋转时间
 5         android:fromDegrees="0"//起始角度
 6         android:pivotX="50%"//起始点的x坐标
 7         android:pivotY="50%"//起始点的y坐标
 8         android:repeatCount="-1"//-1表示无限循环
 9         android:repeatMode="restart"//重复的模式,默认为restart,即重头开始重新运行,可以为reverse即从结束开始向前重新运行。在android:repeatCount大于0或为infinite时生效 
10 android:toDegrees="360">//介绍时的角度 11 12 13 </rotate> 14 </set>
在java代码中引用资源文件     

ImageView green = (ImageView) findViewById(R.id.green); Animation rotate = AnimationUtils.loadAnimation(this,R.anim.rotate);//把资源文件放进Animation的对象中 green.startAnimation(rotate);

 

原文地址:https://www.cnblogs.com/lyd447113735/p/8026417.html