Android:Animator使用指南

(一)快速开始

Animator是属性动画,通过修改对象的属性来达到实现动画的效果。

一段替换Animation全功能的Animator代码:(请使用3.0以上API来执行前面的例子)

        ObjectAnimator animation = ObjectAnimator.ofFloat(view, "alpha", 0f, 1f);
//        ObjectAnimator animation = ObjectAnimator.ofFloat(view, "translationX", 0, 500);
//        ObjectAnimator animation = ObjectAnimator.ofFloat(view, "translationY", 0, 500);
//        ObjectAnimator animation = ObjectAnimator.ofFloat(view, "scaleX", 0, 2f);
//        ObjectAnimator animation = ObjectAnimator.ofFloat(view, "scaleY", 0, 2f);
//        ObjectAnimator animation = ObjectAnimator.ofFloat(view, "rotation", 0, 360f);
//        ObjectAnimator animation = ObjectAnimator.ofFloat(view, "rotationX", 0, 360f);
//        ObjectAnimator animation = ObjectAnimator.ofFloat(view, "rotationY", 0, 360f);
        animation.setDuration(5000);
        animation.start();

以上代码可以实现:透明度,位移,缩放,旋转变化

Animatoin中包含的锚点,可以直接对view进行设置:

        view.setPivotX(0);
        view.setPivotY(0);

虽然属性动画不太适合用XML书写,但是其Java代码的属性难度非常低,很容易理解。

(二)总体脉络

首先从实现一个颜色渐变的动画开始,讲述属性动画的属性是指什么,然后讲述一下如何使用TypeEvaluator。


(三) 颜色的渐变动画

当你想改变一个View的背景颜色的时候,你想到的方法是什么?

view.setBackgroundColor(color);

而对于属性动画而言,你指定一个属性之后,动画框架会反射的去调用你的setXXX来实现对你的属性的修改。

根据这个思路,我们将属性命名为backgroundColor ,于是写下下面的代码:

ObjectAnimator animation = ObjectAni
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值