Android动画之RotateAnimation

Android 3xcn.com@站长2020年8月19日 pm12:50发布1年前 (2023)更新
0
导航号,我的单页导航
目录

Android动画

  1. View Animation 视图动画(Tween Animation 补间动画),只能用来设置View的动画
  2. Drawable Animation 帧动画(Frame动画),一帧帧地显示资源文件中的Drawable
  3. Property Animation 属性动画,在android3.0以上的系统才有。这动画可以设置给任何的Object,包括那些还没有渲染到屏幕的view.

为什么要引入属性动画?

  1. 补间动画只能够作用在View上的
  2. 补间动画只能够实现移动、缩放、旋转和淡入淡出这四种动画操作,不能改变View的背景等
  3. 补间动画只是改变了View的显示效果而已,而不会真正去改变View的属性

View Animation 补间动画

视图动画也叫补间动画,指在一个视图容器中执行一些变换。包含有:位置、大小、旋转、透明 补间动画。

一般通过xml实现,不建议是用android代码实现,因为代码实现的可读性比较差。

补间动画的相关类
  • AlphaAnimation <alpha>放在res/anim/目录下 透明渐变动画效果
  • RotateAnimation <rotate>放在res/anim/目录下 旋转转移动画效果
  • ScaleAnimation <scale>放在res/anim/目录下 缩放动画效果
  • TranslateAnimation <translate>放在res/anim/目录下 移动动画效果
  • AnimationSet <set> 放在res/anim/目录下 持有动画的容器

补间动画之RotateAnimation使用

RotateAnimation动画的实现可以分类两种,一种是使用Java代码实现,另外一种使用xml。

使用java代码实现

RotateAnimation初始化代码片段

    /**
     * init animation 1
     */
    private void initAnimation1() {
        /**
         *   0 - 359度旋转
         *   相对于自身中心位置
         */
        RotateAnimation mRotateAnimation = new RotateAnimation(0, 359,
                Animation.RELATIVE_TO_SELF, 0.5f,
                Animation.RELATIVE_TO_SELF, 0.5f);
        //设置线性插值,可以解决旋转一圈后卡顿问题
        mRotateAnimation.setInterpolator(new LinearInterpolator());
        //设置旋转一圈时间
        mRotateAnimation.setDuration(1000);
        //设置重复旋转次数, Animation.INFINITE表示无限次
        mRotateAnimation.setRepeatCount(Animation.INFINITE);
        //设置旋转模式[restart 重新旋转; reverse 旋转一圈后反转]
        mRotateAnimation.setRepeatMode(Animation.RESTART);

        return;
    }

动画的启动和停止

//启动动画
imageView.startAnimation(mRotateAnimation);

// 停止动画
imageView.clearAnimation();
使用xml实现

res/anim/anim_rotate.xml

<set xmlns:android="http://schemas.android.com/apk/res/android">

    <rotate
        android:duration="1500"
        android:fromDegrees="0"
        android:pivotX="50%"
        android:pivotY="50%"
        android:repeatCount="-1"
        android:repeatMode="reverse"
        android:toDegrees="359" />

    <!--
     duration : 一圈旋转时间
     fromDegrees : 开始角度
     toDegrees : 结束角度
     pivotX : 旋转中心距离view的左顶点为50%距离
     pivotY : 旋转中心距离view的左顶点为50%距离
     repeatCount="-1":重复次数,-1为一直重复
     repeatMode="restart":重复模式,restart从头开始重复 reverse 旋转完后反转旋转
    -->

</set>
    /**
     * init animaton 2
     */
    private void initAnimation2() {
        //加载xml中的动画
        Animation mAnimation = AnimationUtils.loadAnimation(this, R.anim.anim_rotate);
        //设置线性插值
        mAnimation.setInterpolator(new LinearInterpolator());
        return;
    }

参考文章

  1. android动画
  2. [摘抄]Android动画介绍和属性意义介绍(1)
  3. Android中如何使用rotate实现图片不停旋转的效果与动画的停止

版权声明 1、 本站名称 91易搜
2、 本站网址 https://www.91es.com/
3、 本站部分文章来源于网络,仅供学习与参考,如有侵权请留言
4、 本站禁止发布或转载任何违法的相关信息,如有发现请向站长举报
导航号,我的单页导航

暂无评论

评论审核已启用。您的评论可能需要一段时间后才能被显示。

暂无评论...