Android 高级进阶 - 动画之属性动画(Property Animation)

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

前言:

补间动画 pk 属性动画

补间动画不足以覆盖所有动画场景,在功能和扩展方面存在缺陷;属性动画则弥补了这些缺陷。

01、补间动画只对View进行操作;属性动画也能对非View对象进行操作(如自定义View中point对象,自定义View也就有了动画效果)。

02、补间动画只能够实现移动、旋转、缩放、透明度;属性动画则不然。

03、补间动画只改变了View的显示效果,不能改变View的属性(如View的点击事件还保留在原来位置);属性动画则不然。

一、ValueAnimator

其实现机制是不断的对值进行操作,开始值到结束值之间的过渡是通过ValueAnimator来计算的。以及负责播放次数、播放时长、动画监听等。

        // ofFloat 值可以有多个
        ValueAnimator animator = ValueAnimator.ofFloat(0.0f, 100.0f);
        animator.setTarget(imageView);
        animator.setDuration(1000);
        // 可以在回调中不断更新View的多个属性,使用起来更加灵活
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {

            @Override
            public void onAnimationUpdate(ValueAnimator valueAnimator) {
                // 获取当前值
                Float mValue = (Float) valueAnimator.getAnimatedValue();
                // 设置横向偏移量
                imageView.setTranslationX(mValue);
                // 设置纵向偏移量
                imageView.setTranslationY(mValue);
            }
        });
        animator.start();

二、ObjectAnimator

继承自 ValueAnimator。可以直接对任意对象的任意属性进行动画操作。

        ObjectAnimator animator = ObjectAnimator.ofFloat(imageView, "alpha", 0.0f, 1.0f);
        animator.setDuration(5000);
        animator.setRepeatCount(2);
        animator.setInterpolator(new LinearInterpolator());
        animator.start();

三、AnimatorSet

after(Animator anim)   将现有动画插入到传入的动画之后执行

after(long delay)   将现有动画延迟指定毫秒后执行

before(Animator anim)   将现有动画插入到传入的动画之前执行

with(Animator anim)   将现有动画和传入的动画同时执行



猜你喜欢

转载自blog.csdn.net/zp0119/article/details/79085477