Android动画实战

 本文来自刘兆贤_Java高级,Android旅行,Android进阶-CSDN博客 ,引用必须注明出处!

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

动画包含Frame、Tween和属性三种。

Frame指:帧动画,如电影就是帧动画,App上最常见的是圆圈进行条的实现。

Tween指:补间动画,针对一张图片,进行渐变(Alpha)、位移(Translate)、旋转(Rotate)、伸缩(Scale)处理,实现简单的帧动画的效果(如圆圈进度条)。

属性指:跟补间动画类似,但它是真实改变了View的属性。

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。实现方法,主要使用AnimationUtils来加载xml动画文件

配置xmll,设置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种动画同理。

属性动画,是使用ObjectAnimator来定义动画结构。

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常量速率运动等。

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

刘兆贤

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值