Android动画实战

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/liuxian13183/article/details/88120546

 本文来自http://blog.csdn.net/liuxian13183/ ,引用必须注明出处!

从表现形式来看,Android开发的功能,除了底层和基础的功能之外,剩下的就是炫酷功能的实现。底层功能通常用C语言使用、Java层调用,基本功能有各种各样的控件,而炫酷功能的实现,往往一千个设计师就会有一千种设计要求。

动画包含Frame和Tween两种。Frame指(序列)帧动画(多张图片),如电影就是帧动画,App上最常见的是圆圈进行条的实现;Tween指针对一张图片,进行渐变(Alpha)、位移(Translate)、旋转(Rotate)、伸缩(Scale)处理,实现简单的帧动画的效果(如圆圈进度条)。区别就是:Tween比较轻便,资源消耗少,只能实现较为简单的动画;而Frame比较沉重,资源消耗多,但能实现比较复杂的动画。两者没有绝对的好与不好,但通常情况下使用Tween比较多,即Animatation。

先讲讲比较简单的帧动画。

<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
    android:oneshot="true">
    <item
        android:duration="83"
        android:drawable="@mipmap/icon_add_blood_small_00" />
    <item
        android:duration="83"
        android:drawable="@mipmap/icon_add_blood_small_01" />
    <item
        android:duration="83"
        android:drawable="@mipmap/icon_add_blood_small_02" />
    <item
        android:duration="83"
        android:drawable="@mipmap/icon_add_blood_small_03" />
</animation-list>

以上是一个用xml编写的drawable文件,使用animation-list来构建整个文件,oneshot=true代表整张图片只执行1次,否则就是循环播放;item中的drawable指当前图片,duration指当前图片的播放时长。

接下来说Tween。实现方法有2种,一种使用AnimationUtils来加载xml动画文件,一种是使用ObjectAnimator来定义动画结构。

先说xml,设置duration即运行时间,fillBefore=true代表结束后停留在第1帧,当然fillAfter=true代表结束后停留在最后1帧,formAlpha指开始渐变的透明度,toAlpha即结束渐变时的透明度。

<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="500"
    android:fillBefore="true"
    android:fromAlpha="0.0"
    android:toAlpha="1.0">

</alpha>

将xml动画文件转化成动画对象,this指context,hammerIv指绑定的ImageView,执行动画即可。

   AlphaAnimation alphaAnim = (AlphaAnimation) AnimationUtils.loadAnimation(this, R.anim.live_stage_hammer_alpha);
        hammerIv.startAnimation(alphaAnim);

其他3种动画同理。

扫描二维码关注公众号,回复: 5943493 查看本文章

Rotate:fromDegrees/toDegrees指开始/结束角度,pivoteX/pivoteY指旋转原点,startOffset指延迟播放时间(第1个动画播放250ms,则第2个动画延迟250ms播放。。。)

多个动画轮流播放。使用set构建整个结构,

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <alpha
        android:duration="250"
        android:fillAfter="true"
        android:fromAlpha="0.0"
        android:toAlpha="1.0" />
    <rotate
        android:duration="350"
        android:fillAfter="true"
        android:fromDegrees="0"
        android:pivotX="100%"
        android:pivotY="100%"
        android:startOffset="250"
        android:toDegrees="70" />
    <rotate
        android:duration="100"
        android:fillAfter="true"
        android:fromDegrees="0"
        android:pivotX="100%"
        android:pivotY="100%"
        android:startOffset="700"
        android:toDegrees="90" />
</set>

代码实现,使用AnimatorSet作为构建对象,依次播放如下动画即可。PS:ObjectAnimator里的values可写多个,开始-结束-开始-结束。。。以这样的节奏执行。

        AnimatorSet animatorSet = new AnimatorSet();
        hammerIv.setPivotX(hammerIv.getWidth());
        hammerIv.setPivotY(hammerIv.getHeight());
        ObjectAnimator alphaAnimator = ObjectAnimator.ofFloat(hammerIv, "alpha", 0.0f, 1.0f);
        alphaAnimator.setDuration(250);
        ObjectAnimator upAnimator = ObjectAnimator.ofFloat(hammerIv, "rotation", 0f, 70f);
        upAnimator.setDuration(350);
        ObjectAnimator downAnimator = ObjectAnimator.ofFloat(hammerIv, "rotation", 0, 90, 0);
        downAnimator.setDuration(100);
        downAnimator.setInterpolator(new FastOutSlowInInterpolator());
        /**
         * BounceInterpolator 动画播放结束后,有回弹效果
         * CycleInterpolator 动画播放结束后,继续沿正弦曲线运动设置的弧度
         * FastOutSlowInInterpolator 动画快出慢进
         */
        animatorSet.playSequentially(alphaAnimator, upAnimator, downAnimator);
        animatorSet.start();

Interpolator指动画控制器,如上注释所述,还有LinearInterpolator常量速率运动等。

如果你对其他属性比较感兴趣,不如在评论区一起来探讨。

猜你喜欢

转载自blog.csdn.net/liuxian13183/article/details/88120546