【达内课程】Android中的动画

分类

1、帧动画
帧数:fps frame per second
2、补间动画

这里写图片描述

补间动画

淡入淡出alpha

res文件夹下new——Android Resource File
这里写图片描述
这里写图片描述

alpha

<set xmlns:android="http://schemas.android.com/apk/res/android">
    <alpha
        android:fromAlpha="0"
        android:toAlpha="1"
        android:duration="5000"
        />
</set>

执行动画代码

Animation animation = AnimationUtils.loadAnimation(this,R.anim.alpha);
tv_hello_world.startAnimation(animation);

效果图
这里写图片描述

旋转rotate

这里写图片描述
新建rotate文件,右键anim——new——Animation Resource File
这里写图片描述

rotate

<set xmlns:android="http://schemas.android.com/apk/res/android">
    <rotate
        android:fromDegrees="0"
        android:toDegrees="360"
        android:duration="5000"/>
</set>

执行动画代码

Animation rotate = AnimationUtils.loadAnimation(this,R.anim.rotate);
                tv_hello_world.startAnimation(rotate);

效果图

这里写图片描述

修改旋转点

<set xmlns:android="http://schemas.android.com/apk/res/android">
    <rotate
        android:fromDegrees="0"
        android:toDegrees="360"
        android:duration="5000"
        android:pivotX="50%"
        android:pivotY="50%"/>
</set>

效果图
这里写图片描述

缩放动画scale

这里写图片描述
创建scale文件

<set xmlns:android="http://schemas.android.com/apk/res/android">
    <scale
        android:fromXScale="0"
        android:toXScale="100%"

        android:fromYScale="0"
        android:toYScale="100%"

        android:pivotX="50%"
        android:pivotY="50%"

        android:duration="5000"/>
</set>

执行动画代码

Animation scale = AnimationUtils.loadAnimation(this,R.anim.scale);
                tv_hello_world.startAnimation(scale);

效果图
这里写图片描述

平移动画

translate
这里写图片描述

<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate
        android:fromXDelta="0"
        android:toXDelta="100%"

        android:fromYDelta="0"
        android:toYDelta="100%"

        android:duration="5000"/>
</set>

执行动画代码

Animation translate = AnimationUtils.loadAnimation(this,R.anim.translate);
                tv_hello_world.startAnimation(translate);

效果图
这里写图片描述

这里写图片描述

现在我们会发现执行完动画效果,控件会闪退回控件初始的状态,尝试在根节点增加以下属性,会让控件停留在最后的位置

android:fillAfter="true"

效果图
这里写图片描述

不过应该让动画回到最开始的位置,否则再执行其他动画会出现各种奇怪的bug,可以设置让动画重复执行一次,回到开始位置

<set xmlns:android="http://schemas.android.com/apk/res/android"
    >
    <translate
        android:repeatCount="1"
        android:repeatMode="reverse"

        android:fromXDelta="0"
        android:toXDelta="100%"

        android:duration="5000"/>
</set>

效果图
这里写图片描述

综合应用

set

<set xmlns:android="http://schemas.android.com/apk/res/android">
    <alpha
        android:fromAlpha="0.2"
        android:toAlpha="1"
        android:duration="5000"/>
    <rotate
        android:fromDegrees="0"
        android:toDegrees="1800"
        android:pivotX="50%"
        android:pivotY="50%"
        android:duration="5000"/>

    <scale
        android:fromXScale="0"
        android:toYScale="100%"

        android:fromYScale="0"
        android:toXScale="100%"

        android:pivotX="50%"
        android:pivotY="50"

        android:duration="5000"/>

</set>

效果图
这里写图片描述

可以设置动画的执行顺序,给透明度增加startOffset属性

<set xmlns:android="http://schemas.android.com/apk/res/android">
    <alpha
        android:startOffset="5000"
        android:fromAlpha="0.2"
        android:toAlpha="1"
        android:duration="5000"/>
    <rotate
        android:fromDegrees="0"
        android:toDegrees="1800"
        android:pivotX="50%"
        android:pivotY="50%"
        android:duration="5000"/>

    <scale
        android:fromXScale="0"
        android:toYScale="100%"

        android:fromYScale="0"
        android:toXScale="100%"

        android:pivotX="50%"
        android:pivotY="50"

        android:duration="5000"/>

</set>

效果图
这里写图片描述

我们看旋转有一个加速度,这个属性也可以设置,由于不常用,暂不记录

猜你喜欢

转载自blog.csdn.net/u010356768/article/details/80908163