gpt4 book ai didi

android - 如何将 ObjectAnimator 重置为初始状态?

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:31:32 25 4
gpt4 key购买 nike

我想用 scaleX 和 scaleY 振动一个 View ,我正在用这段代码来做,但问题是有时 View 没有正确重置,它显示了应用的比例......

我希望当动画结束时,必须始终看到 View 的原始状态

这是代码:

                ObjectAnimator scaleX = ObjectAnimator.ofFloat(view, "scaleX", 1f, 0.9f);
scaleX.setDuration(50);
scaleX.setRepeatCount(5);
scaleX.setRepeatMode(Animation.REVERSE);
ObjectAnimator scaleY = ObjectAnimator.ofFloat(view, "scaleY", 1f, 0.9f);
scaleY.setDuration(50);
scaleY.setRepeatCount(5);
scaleY.setRepeatMode(Animation.REVERSE);
set.play(scaleX).with(scaleY);
set.start();

谢谢

最佳答案

对于ValueAnimator和ObjectAnimator可以这样试试:

animator.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
animation.removeListener(this);
animation.setDuration(0);
((ValueAnimator) animation).reverse();
}
});

更新在 Android 7 上它不起作用。使用插值器的最佳方式。

public class ReverseInterpolator implements Interpolator {

private final Interpolator delegate;

public ReverseInterpolator(Interpolator delegate){
this.delegate = delegate;
}

public ReverseInterpolator(){
this(new LinearInterpolator());
}

@Override
public float getInterpolation(float input) {
return 1 - delegate.getInterpolation(input);
}
}

在你的代码中

animator.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
animation.removeListener(this);
animation.setDuration(0);
animation.setInterpolator(new ReverseInterpolator());
animation.start();
}
});

关于android - 如何将 ObjectAnimator 重置为初始状态?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27616444/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com