您的位置:首页 > 移动开发 > Android开发

Android实现ImageView的旋转动画

2015-12-29 16:53 429 查看
一、View基础动画 RotationAnimation
1、动态生成:RotateAnimation (float fromDegrees, float toDegrees, int pivotXType, float pivotXValue, int pivotYType, float pivotYValue)

参数说明:

float fromDegrees:旋转的开始角度。 
float toDegrees:旋转的结束角度。 
int pivotXType:X轴的伸缩模式,可以取值为ABSOLUTE、RELATIVE_TO_SELF、RELATIVE_TO_PARENT。 
float pivotXValue:X坐标的伸缩值。 
int pivotYType:Y轴的伸缩模式,可以取值为ABSOLUTE、RELATIVE_TO_SELF、RELATIVE_TO_PARENT。 
float pivotYValue:Y坐标的伸缩值。

2、使用动画文件res/anim/anim_round_rotate.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"  <!--0~359防止卡顿-->
android:pivotX="50%"     <!--设置旋转中心点为控件中心-->
android:pivotY="50%"
android:duration="1000"
android:repeatCount="-1" /> <!--设置不断旋转-->
</set>
3、如何给ImageView加上动画效果?

Animation circle_anim = AnimationUtils.loadAnimation(context, R.anim.ddqb_anim_round_rotate);
LinearInterpolator interpolator = new LinearInterpolator();  //设置匀速旋转,在xml文件中设置会出现卡顿
circle_anim.setInterpolator(interpolator);
if (circle_anim != null) {
img_loading_circle.startAnimation(circle_anim);  //开始动画
}


二、利用属性动画实现立体的旋转动画

属性动画ObjectAnimation实现旋转,主要用到rotationX和rotationY这两个属性,没有rotationZ这个属性。

ObjectAnimator icon_anim = ObjectAnimator.ofFloat(img_loading_icon, "rotationY", 0.0F, 359.0F);//设置Y轴的立体旋转动画</span></span>
icon_anim.setRepeatCount(-1); icon_anim.setDuration(1000); icon_anim.setInterpolator(interpolator); //设置匀速旋转,不卡顿 icon_anim.start();



三、如何结束动画?
1、对于RotationAnimation,直接调用view.clearAnimation() 清除动画;

2、对于ObjectAnimation,则调用ObjectAnimation对象的end()方法结束。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  动画 imageview