如何将ObjectAnimator重置为其初始状态? [英] How to reset ObjectAnimator to it's initial status?
本文介绍了如何将ObjectAnimator重置为其初始状态?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想用scaleX和scaleY振动视图,并使用此代码来执行此操作,但是问题是有时视图未正确重置,并且显示了应用了比例的情况...
I want to vibrate a view with scaleX and scaleY, and I am doing it with this code, but the problem is that sometimes the view is not correctly reset, and it shows with the scale applied...
我希望动画结束时,必须始终以其原始状态查看视图
I want that when the animation ends, the view must be seen with its original status always
这是代码:
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可以这样尝试:
For ValueAnimator and ObjectAnimator can be like this a try:
animator.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
animation.removeListener(this);
animation.setDuration(0);
((ValueAnimator) animation).reverse();
}
});
更新 在Android 7上不起作用. 最好的方法是使用插值器.
UPDATE On Android 7 it doesn't work. Best way use the interpolator.
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();
}
});
这篇关于如何将ObjectAnimator重置为其初始状态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文