zoukankan      html  css  js  c++  java
  • Android补间动画(附带Demo案例)

    Demo源码

    请点击:Github-Animation

    补间动画(Tween Animation)

    • 分类:透明动画、旋转动画、位移动画、缩放动画、组合动画
    • 写法:代码中动态设置属性、使用xml文件定义属性 两种方式

    补间动画不会改变控件的位置

    如果需要使用XML方式定义补间动画
    首先,在资源路径 res 下创建目录:anim(名字不能错)
    其次,在 res/anim 目录下创建xml文件,根节点根据对应的补间动画定义(文章中有具体的代码示例)

    透明动画(AlphaAnimation)

    1. 代码示例(代码中设置属性)

    //创建一个透明动画
    Animation animation = new AlphaAnimation(1, 0);
    //添加属性:设置重复播放一次(总共播放两次)
    animation.setRepeatCount(1);
    //设置动画重复播放的模式
    animation.setRepeatMode(Animation.RESTART);
    //添加属性:动画播放时长(两次播放时长)
    animation.setDuration(2000);
    animation.setFillAfter(true);
    //设置动画
    ivView.startAnimation(animation);
    //设置播放监听(监听:开始播放时、播放结束时、重复播放时)
    //animation.setAnimationListener(Animation.AnimationListener listener);
    

    2. 代码示例(XML中设置属性)

    • R.anim.anim_my_alpha
    <?xml version="1.0" encoding="utf-8"?>
    <alpha xmlns:android="http://schemas.android.com/apk/res/android"
           android:duration="2000"
           android:fromAlpha="1.0"
           android:repeatCount="1"
           android:fillAfter="false"
           android:repeatMode="reverse"
           android:toAlpha="0.0">
    </alpha>
    
    • 调用XML
    Animation animation = AnimationUtils.loadAnimation(this, R.anim.anim_my_alpha);
    ivView.startAnimation(animation);
    

    旋转动画(RotateAnimation)

    1. 代码示例(代码中设置属性)

    RotateAnimation animation = new RotateAnimation(0, 360);
    //RotateAnimation animation = new RotateAnimation(0, 360,
    //        Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
    animation.setDuration(2000);
    animation.setRepeatCount(1);
    animation.setRepeatMode(Animation.RESTART);
    animation.setFillAfter(true);
    ivView.startAnimation(animation);
    

    2. 代码示例(XML中设置属性)

    • R.anim.anim_my_rotate
    <?xml version="1.0" encoding="utf-8"?>
    <rotate xmlns:android="http://schemas.android.com/apk/res/android"
            android:duration="2000"
            android:fromDegrees="0"
            android:pivotX="50%"
            android:pivotY="50%"
            android:repeatCount="2"
            android:fillAfter="false"
            android:repeatMode="reverse"
            android:toDegrees="360">
        <!--以自身中心为点,旋转360度-->
        <!--android:pivotX="50%"-->
        <!--android:pivotY="50%"-->
        <!--加'p'是以父窗体的中心点为参照物-->
        <!--android:pivotX="50%p"-->
        <!--android:pivotY="50%p"-->
    </rotate>
    
    • 调用XML
    Animation animation = AnimationUtils.loadAnimation(this, R.anim.anim_my_rotate);
    ivView.startAnimation(animation);
    

    位移动画(TranslateAnimation)

    1. 代码示例(代码中设置属性)

    //如果参照物是:Animation.RELATIVE_TO_PARENT,那么移动的距离:是父窗体宽高的多少倍
    //TranslateAnimation animation = new TranslateAnimation(Animation.RELATIVE_TO_PARENT, 0,
    //        Animation.RELATIVE_TO_PARENT, 0.3f, Animation.RELATIVE_TO_PARENT, 0, Animation.RELATIVE_TO_PARENT, 0.4f);
    TranslateAnimation animation = new TranslateAnimation(300, -300, 0, 0);
    animation.setDuration(2000);
    animation.setRepeatCount(1);
    animation.setRepeatMode(Animation.RESTART);
    //设置动画执行完毕后,是否停留到最后的位置上
    //true:停留在最后的位置上; false:返回原来的位置上
    //即便停留在最后的位置上,但控件的实际位置并没有改变(可以通过给控件设置点击事件判断)
    animation.setFillAfter(true);
    ivView.startAnimation(animation);
    

    2. 代码示例(XML中设置属性)

    • R.anim.anim_my_translate
    <?xml version="1.0" encoding="utf-8"?>
    <translate xmlns:android="http://schemas.android.com/apk/res/android"
               android:duration="2000"
               android:fillAfter="false"
               android:fromXDelta="0"
               android:fromYDelta="0"
               android:repeatCount="1"
               android:repeatMode="reverse"
               android:toXDelta="0"
               android:toYDelta="30%p">
        <!--X轴不变,Y轴方向向下移动距离:父窗体高度的30%-->
        <!--带%:相当于父窗体或自身的宽高。不带%:直接就是移动多少像素-->
        <!--带p:相对于父窗体的。不带p:相对于自己-->
    </translate>
    
    • 调用XML
    Animation animation = AnimationUtils.loadAnimation(this, R.anim.anim_my_translate);
    ivView.startAnimation(animation);
    

    缩放动画(ScaleAnimation)

    1. 代码示例(代码中设置属性)

    ScaleAnimation animation = new ScaleAnimation(1, 2, 1, 2,
                    Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
    animation.setDuration(2000);
    animation.setRepeatCount(1);
    animation.setRepeatMode(Animation.RESTART);
    animation.setFillAfter(true);
    ivView.startAnimation(animation);
    

    2. 代码示例(XML中设置属性)

    • R.anim.anim_my_scale
    <?xml version="1.0" encoding="utf-8"?>
    <scale xmlns:android="http://schemas.android.com/apk/res/android"
           android:duration="2000"
           android:fromXScale="1"
           android:fromYScale="1"
           android:pivotX="50%"
           android:pivotY="50%"
           android:repeatCount="1"
           android:fillAfter="false"
           android:repeatMode="reverse"
           android:toXScale="2"
           android:toYScale="2">
        <!--以自身为参照物,X、Y轴均扩大2倍-->
    </scale>
    
    • 调用XML
    Animation animation = AnimationUtils.loadAnimation(this, R.anim.anim_my_scale);
    ivView.startAnimation(animation);
    

    组合动画(AnimationSet)

    1. 代码示例(代码中设置属性)

    AnimationSet set = new AnimationSet(true);
    //透明
    AlphaAnimation alpha = new AlphaAnimation(1, 0);
    alpha.setRepeatCount(1);
    alpha.setRepeatMode(Animation.RESTART);
    set.addAnimation(alpha);
    //旋转
    RotateAnimation rotate = new RotateAnimation(0, 360,
                    Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
    rotate.setRepeatCount(1);
    rotate.setRepeatMode(Animation.RESTART);
    set.addAnimation(rotate);
    //位移
    TranslateAnimation translate = new TranslateAnimation(0, 0, -200, 200);
    translate.setRepeatCount(1);
    translate.setRepeatMode(Animation.RESTART);
    set.addAnimation(translate);
    //缩放
    ScaleAnimation scale = new ScaleAnimation(1, 2, 1, 2,
                    Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
    scale.setRepeatCount(1);
    scale.setRepeatMode(Animation.RESTART);
    set.addAnimation(scale);
    
    set.setDuration(3000);
    ivView.startAnimation(set);
    

    2. 代码示例(XML中设置属性)

    • R.anim.anim_my_set
    <?xml version="1.0" encoding="utf-8"?>
    <set xmlns:android="http://schemas.android.com/apk/res/android">
        <alpha
                android:duration="2000"
                android:fromAlpha="1.0"
                android:repeatCount="1"
                android:fillAfter="false"
                android:repeatMode="reverse"
                android:toAlpha="0.0"/>
        <rotate
                android:duration="2000"
                android:fromDegrees="0"
                android:pivotX="50%"
                android:pivotY="50%"
                android:repeatCount="1"
                android:fillAfter="false"
                android:repeatMode="reverse"
                android:toDegrees="360"/>
        <translate
                android:duration="2000"
                android:fromXDelta="0"
                android:fromYDelta="0"
                android:fillAfter="false"
                android:repeatCount="1"
                android:repeatMode="reverse"
                android:toXDelta="0"
                android:toYDelta="30%p"/>
        <scale
                android:duration="2000"
                android:fromXScale="1"
                android:fromYScale="1"
                android:pivotX="50%"
                android:pivotY="50%"
                android:repeatCount="1"
                android:fillAfter="false"
                android:repeatMode="reverse"
                android:toXScale="2"
                android:toYScale="2"/>
    </set>
    
    • 调用XML
    Animation animation = AnimationUtils.loadAnimation(this, R.anim.anim_my_set);
    ivView.startAnimation(animation);
    

    PS:
    补间动画:我踩过的坑

    属性动画:请点击此处

    帧动画:请点击此处

  • 相关阅读:
    python检测挖矿特征的几种方式
    python检测当前端口是否使用
    matlab界面UI设计资料
    python中struct.pack()函数和struct.unpack()函数
    网络编程:主机字节序和网络字节序
    【原创】python中文编码问题深入分析(三):python2.7文件读写中文编码问题
    【原创】python中文编码问题深入分析(二):print打印中文异常及显示乱码问题分析与解决
    ivew定制主题 less ^3.0 时报错 .bezierEasingMixin(); Inline JavaScript is not enabled. Is it set in your options?
    Vue子组件中 data 从props中动态更新数据
    Vue 自动吸顶
  • 原文地址:https://www.cnblogs.com/io1024/p/11584606.html
Copyright © 2011-2022 走看看