取消android所有动画,android – 动画取消动画

正如@Gil正确指出的那样,您必须处理自定义Interpolator实现.好消息是你实际上并不需要自己实现所有东西.您可以组合2种不同的插值公式:加速/减速主动画,减速插补器取消.

基本上这就是你要找的东西:

正常加速/减速动画:

在中间的某处取消它时的动画:

这是我的快速插补器实现:

static class MyInterpolator extends AccelerateDecelerateInterpolator {

private float phaseShift = 0f;

private boolean isCancelled = false;

private float lastInput = 0f;

/**

* 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.

*/

@Override

public float getInterpolation(float input) {

lastInput = input;

if(!isCancelled)

{

return super.getInterpolation(input);

}

else

{

return getCancellationInterpolation(input) - phaseShift;

}

}

public void cancel()

{

isCancelled = true;

this.phaseShift = getCancellationInterpolation(lastInput) - super.getInterpolation(lastInput);

}

private float getCancellationInterpolation(float input)

{

return (1.0f - (1.0f - input) * (1.0f - input));

}

}

如您所见,我使用默认插值进行普通动画,并在取消动画时切换减速.显然这段代码并不完美(它不会重置phaseShift值和isCancelled标志,如果你使用重复模式会导致错误计算),但这是你希望自己能够找到的东西:)

我创建了示例项目on GitHub,因此您可以看到它的外观

跟进

我用公式玩了一下,并采用了DecelerateInterpolator实现的第二部分.我介绍了因子参数,它可以帮助您控制取消发生的速度(某种牵引力).设定因子为1.5给我这个:

正如你所看到的,当我在~0.5点点击取消时,动画会更快地被取消(因此它不会像前面的例子那样一直到0.7的距离).它给人一种更好的真实感觉.更高的因素 – 动画停止的速度更快.

这是一个更新的插补器:

static class MyInterpolator extends AccelerateDecelerateInterpolator {

......

private float factor = 1.5f;

.......

private float getCancellationInterpolation(float input)

{

if(factor == 1)

{

return (1.0f - (1.0f - input) * (1.0f - input));

}

else

{

return (float)(1.0f - Math.pow((1.0f - input), 2 * factor));

}

}

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值