Android 动画 - RoateAnimation 旋转动画

本文详细介绍了Android中RotateAnimation的使用,包括通过XML文件和Java代码两种方式创建旋转动画,强调了旋转角度、旋转轴心、动画时长等关键参数的设置,并通过实例解释了旋转方向和旋转中心对动画效果的影响。

RoateAnimation:旋转动画,ViewAnimation的小分支
同样,创建RoateAnimation有两种方式
1. XML文件+Java代码
2. Java代码方式

XML文件+Java代码

示例图片:
这里写图片描述

roate.xml

<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
        android:duration="3000"
        android:fillAfter="true"
        android:fillBefore="false"
        android:fromDegrees="0"
        android:interpolator="@android:anim/linear_interpolator"
        android:pivotX="50%"
        android:pivotY="50%"
        android:repeatCount="-1"
        android:repeatMode="reverse"
        android:startOffset="2000"
        android:toDegrees="180"/>

Java代码

        RotateAnimation rotateAnimation = (RotateAnimation) AnimationUtils.loadAnimation(XmlViewAnimationActivity.this, R.anim.roate);
        mIvImg.startAnimation(rotateAnimation);
  • android:duration:动画持续时长
  • android:fillAfter:动画结束之后是否保持动画的最终状态;true,表示保持动画的最终状态
  • android:fillBefore:动画结束之后是否保持动画开始前的状态;true,表示恢复到动画开始前的状态
  • android:fromDegrees:动画开始的角度
  • android:interpolator:动画插值器。是实现动画不规则运动的一种方式,后面讲到
  • android:pivotX:缩放中心坐标的X值,取值类型有三种:数字;百分比;百分比+”p”;
    • 数字:例如50.0,这里的单位是px像素
    • 百分比:例如50%,这里是相对于自己控件宽度的百分比,实际的值是mIvImg.getWidth()*50%;
    • 百分比+”p”:例如50%p,这里是表示相对于自己控件的父控件的百分比,
  • android:pivotY:同上
  • android:repeatCount:动画重复的次数。指定动画重复播放的次数;如果你需要无限循环播放,请填写一个小于0的数值,一般写-1
  • android:repeatMode:动画重复的Mode,有reverse和restart两种,效果看后面
  • android:startOffset:动画播放延迟时长,就是调用start之后延迟多少时间播放动画
  • android:toDegrees:动画旋转的目标角度
注意:这里有个旋转问题标准:
  • 原控件的初始位置的角度是0度,当然360度的倍数也是和原始位置角度相同;
  • 如果toDegrees的参数大于fromDegrees,那么就是顺时针旋转;
  • 如果fromDegrees的参数小于toDegrees,那么就是逆时针旋转;

当旋转轴心在自己控件中心时
这里写图片描述

当旋转轴心在屏幕右下角时
这里写图片描述

其他参数都在前面讲过了, 这里唯一要注意的是:初始角度:fromDegrees、目标角度:toDegrees
当android:fromDegrees=”0”,android:toDegrees=”-180”时;
这里写图片描述

roate.xml

<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
        android:duration="3000"
        android:fillAfter="true"
        android:fillBefore="false"
        android:fromDegrees="0"
        android:interpolator="@android:anim/linear_interpolator"
        android:pivotX="50%"
        android:pivotY="50%"
        android:repeatCount="-1"
        android:repeatMode="reverse"
        android:startOffset="2000"
        android:toDegrees="-360"/>

一个以自己控件中心为圆心,逆时针方向旋转一圈的动画

另外举一个不是圆心不在自己控件上的例子
圆心:android:pivotX=”100%p”,android:pivotY=”100%p”。动画控件父控件的右下角,当前的父控件是整个屏幕,所以这时候这个点就是屏幕的右下角
旋转度数:android:fromDegrees=”0”,android:toDegrees=”360”。围绕圆心,顺时针方向旋转一周
这里写图片描述

roate.xml

<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
        android:duration="3000"
        android:fillAfter="true"
        android:fillBefore="false"
        android:fromDegrees="0"
        android:interpolator="@android:anim/linear_interpolator"
        android:pivotX="100%p"
        android:pivotY="100%p"
        android:repeatCount="-1"
        android:repeatMode="reverse"
        android:startOffset="2000"
        android:toDegrees="360"/>

一个以控件的父控件的右下角为圆心,顺时针方向旋转一周的旋转动画


Java代码方式创建动画

用XML文件的形式创建会提供设置ToateAnimation相应的属性,同样Java代码也提供了相应的方法去设置

    public void startRotateAnimation(View view) {
        /**
         * RotateAnimation第一种构造
         *
         * @param fromDegrees 开始的角度,控件正常显示的位置是0度,按顺时针方向旋转是正数,逆时针方向旋转是负数
         * @param toDegrees 结束的角度,如果toDegrees的值大于fromDegrees,则是围绕轴心顺时针旋转;小于则反
         *  这里还有一个默认旋转轴心的问题, 默认是控件的左上角
         */
        RotateAnimation rotateAnimation = new RotateAnimation(0f, 45f);

        /**
         * RotateAnimation第二种构造在第一种构造的基础上增加了可以指定旋转的轴心位置
         *
         * @param fromDegrees 开始的角度, 同上
         * @param toDegrees 结束的角度,同上
         *
         * 旋转的轴心,一个点需要需要一个坐标(x,y)确定, 控件的左上角其实位置是(0,0),控件往右是X正方向, 控件往下是Y的正方向;
         * @param pivotX  pivotX是旋转轴心在X方向上的位置,单位是px像素点,
         * @param pivotY  pivotY是旋转轴心在Y方向上的位置,单位是px像素点,
         */
        RotateAnimation rotateAnimation1 = new RotateAnimation(0f, 45f, mIvImg.getWidth() / 2f, mIvImg.getHeight() / 2f);

        /**
         * RotateAnimation第三种构造在第二种构造的基础上增加了,可以通过多种方式来指定轴心位置,通过Type来约束
         *
         * @param fromDegrees 开始的度数
         * @param toDegrees 结束的度数
         * Type:Animation.ABSOLUTE:绝对,如果设置这种类型,后面pivotXValue取值就必须是像素点;比如:控件X方向上的中心点,pivotXValue的取值mIvImg.getWidth() / 2f
         *            Animation.RELATIVE_TO_SELF:相对于控件自己,设置这种类型,后面pivotXValue取值就会去拿这个取值是乘上控件本身的宽度;比如:控件X方向上的中心点,pivotXValue的取值0.5f
         *            Animation.RELATIVE_TO_PARENT:相对于它父容器(这个父容器是指包括这个这个做动画控件的外一层控件), 原理同上,
         * @param pivotXValue  配合pivotXType使用,原理在上面
         * @param pivotYType 原理同上
         * @param pivotYValue 原理同上
         */
        RotateAnimation rotateAnimation2 = new RotateAnimation(0f, -85f, RotateAnimation.RELATIVE_TO_SELF, 0.5f, RotateAnimation.RELATIVE_TO_SELF, 0.5f);
        //设置动画持续时长
        rotateAnimation2.setDuration(3000);
        //设置动画结束之后的状态是否是动画的最终状态,true,表示是保持动画结束时的最终状态
        rotateAnimation2.setFillAfter(true);
        //设置动画结束之后的状态是否是动画开始时的状态,true,表示是保持动画开始时的状态
        rotateAnimation2.setFillBefore(true);
        //设置动画的重复模式:反转REVERSE和重新开始RESTART
        rotateAnimation2.setRepeatMode(ScaleAnimation.REVERSE);
        //设置动画播放次数
        rotateAnimation2.setRepeatCount(ScaleAnimation.INFINITE);
        //开始动画
        mIvImg.startAnimation(rotateAnimation2);
        //清除动画
        mIvImg.clearAnimation();
        //同样cancel()也能取消掉动画
        rotateAnimation2.cancel();
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值