Android动画深入分析-属性动画插值器Interpolator 和ViewPropertyAnimator使用

本文深入探讨了Android属性动画的概念,特别聚焦于Interpolator的作用及其实现方式,包括系统提供的多种Interpolator类型,如先加速后减速、加速、减速、反弹等,并介绍了如何自定义Interpolator以实现更复杂的动画效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

我们会见到动画播放由快到慢播放、匀速播放、先加速后减速播放。这些效果都是通过插值器来实现的。

Interpolator 简介

1、作用

可以控制动画的变化速率

2、须知

1、Interpolator并不是属性动画中新增的技术,实际上从Android 1.0版本开始就一直存在Interpolator接口
2、补间动画也是支持这个功能的
3、属性动画中新增了一个TimeInterpolator接口,这个接口是兼容之前的Interpolator的。

3、TimeInterpolator常见的实现类(系统实现好的)

1、AccelerateDecelerateInterpolator 先加速后减速
2、AccelerateInterpolator 加速
3、DecelerateInterpolator 减速
4、BounceInterpolator 反弹(乒乓球垂直落地在反弹、反弹、效果)
5、等等

anim.setInterpolator(new AccelerateInterpolator(2f))
4、自定义属性动画的Interpolator

由于属性动画提供了TimeInterpolator的兼容接口。我们实现即可。

(1)TimeInterpolator的源码

package android.animation;

/**
 * A time interpolator defines the rate of change of an animation. This allows animations
 * to have non-linear motion, such as acceleration and deceleration.
 */
public interface TimeInterpolator {

    /**
     * Maps a value representing the elapsed fraction of an animation to a value that represents
     * the interpolated fraction. This interpolated value is then multiplied by the change in
     * value of an animation to derive the animated value at the current elapsed animation time.
     *
     * @param input A value between 0 and 1.0 indicating our current point
     *        in the animation where 0 represents the start and 1.0 represents
     *        the end
     * @return The interpolation value. This value can be more than 1.0 for
     *         interpolators which overshoot their targets, or less than 0 for
     *         interpolators that undershoot their targets.
     */
    float getInterpolation(float input);
}

1、函数接收一个参数 input
2、参数随着动画的运行而不断变化
3、参数变化有规律,他根据动画的时长从0到1匀速增加。(LinearInterpolator的默认实现 直接返回的input值)

(2)input和fraction的关系

input 决定 fraction的值(不同的插值器实现类的getInterpolation()方法实现不同)

LinearInterpolator的实现
在这里插入图片描述

y=kx 式的函数 匀速动画

AccelerateDecelerateInterpolator的实现
在这里插入图片描述

y=cos((x+1)*π/2)+0.5 (0<x<1) 取值范围内 先加速后减速

函数图:(来自网络)
在这里插入图片描述

(3)自定义简单实现

/**
 * Created by sunnyDay on 2019/6/11 20:08
 */
public class CustomInterpolator implements TimeInterpolator {
    @Override
    public float getInterpolation(float input) {
        return (float) Math.sin(Math.PI*input);
    }
}

ViewPropertyAnimator

属性动画的机制已经不是再针对于View而进行设计的了,而是一种不断地对值进行操作的机制,它可以将值赋值到指定对象的指定属性上。但是,在绝大多数情况下,我相信大家主要都还是对View进行动画操作的。Android开发团队也是意识到了这一点,没有为View的动画操作提供一种更加便捷的用法确实是有点太不人性化了,于是在Android 3.1系统当中补充了ViewPropertyAnimator这个机制。

1、栗子
 button = findViewById(R.id.btn);
        button.animate() // 链式调用 返回ViewPropertyAnimator 对象
                .alpha(1)
                .alpha(0)
                .alpha(1)
                .rotationX(360)// 沿着X轴旋转360度
                .setDuration(5000);

1、整个ViewPropertyAnimator的功能都是建立在View类新增的animate()方法之上的
2、链式调用
3、无需开启动画即可(默认会开启)我们也可手动开启。

end

参考:
https://blog.csdn.net/guolin_blog/article/details/44171115
安卓开发艺术探索

同时感谢郭神分享!!!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值