- RotateAnimation:旋转动画,顾名思义,使用该动画,view能产生旋转效果
比如QQ音乐播放状态的专辑
- 看一下xml文件:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<rotate
android:duration="5000"
android:fillAfter="true"
android:fromDegrees="0"
android:pivotX="50%"
android:pivotY="50%"
android:repeatMode="restart"
android:repeatCount="infinite"
android:toDegrees="360" />
</set>
属性解释:
android:fromDegrees=”0” //从多少度开始旋转
android:toDegrees=”360” //旋转到多少度结束
android:pivotX=”50%” // 相对x轴起始位置开始执行动画,取值(0-100%)
android:pivotY=”50%” // 相对y轴起始位置开始执行动画,取值(0-100%)
android:repeatMode=”restart” //重复模式为:重复动画
android:repeatCount=”infinite”//重复次数:无限次
- Java代码调用xml文件:
/**
* 旋转动画
*
* @param context
* @param view 目标view
*/
public static void startRotateAnim(Context context, View view) {
Animation animation = AnimationUtils.loadAnimation(context, R.anim.anim_rotate);
animation.setInterpolator(new LinearInterpolator());
if (view != null)
view.startAnimation(animation);
}
- 效果如下:
- 纯Java代码动画:
public static void animRotate(View view){
RotateAnimation animation = new RotateAnimation(0,360f,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
animation.setDuration(5000);
animation.setFillAfter(true);
animation.setInterpolator(new LinearInterpolator());
animation.setRepeatMode(Animation.RESTART);
animation.setRepeatCount(Animation.INFINITE);
if (view != null)
view.startAnimation(animation);
}