有谁知道如何在ViewPropertyAnimator中重复制作动画?
我已尝试过一切,但没有任何效果,ViewPropertyAnimator不接受其他类型动画的方法
答案 0 :(得分:0)
在致电ViewPropertyAnimator
之前包裹start()
即可。
下面我写了一个包装类,它将在动画结束时重复动画。您可以修改课程以满足您的需求。
package main.java.app.com.animation;
import android.animation.Animator;
import android.support.v4.view.ViewPropertyAnimatorListener;
import android.view.ViewPropertyAnimator;
/**
* Created by nikola on 9/7/14.
*/
public class RepeatableViewPropertyAnimatorWrapper implements Animator.AnimatorListener {
private static RepeatableViewPropertyAnimatorWrapper mInstance;
private ViewPropertyAnimator mViewPropertyAnimator;
private Animator.AnimatorListener mViewPropertyAnimatorListener;
private boolean willRepeat = true;
private Animator mCurrentAnimator;
public static RepeatableViewPropertyAnimatorWrapper wrap(ViewPropertyAnimator animator){
mInstance = new RepeatableViewPropertyAnimatorWrapper(animator);
return mInstance;
}
public static RepeatableViewPropertyAnimatorWrapper withAnimatorListener(Animator.AnimatorListener animatorListener){
mInstance.setAnimatorListener(animatorListener);
return mInstance;
}
private void setAnimatorListener(Animator.AnimatorListener animatorListener) {
mViewPropertyAnimatorListener = animatorListener;
}
public RepeatableViewPropertyAnimatorWrapper(ViewPropertyAnimator animator){
mViewPropertyAnimator = animator;
}
public RepeatableViewPropertyAnimatorWrapper(ViewPropertyAnimator animator, Animator.AnimatorListener animatorListener){
mViewPropertyAnimator = animator;
mViewPropertyAnimatorListener = animatorListener;
}
public boolean isRepeating() {
return willRepeat;
}
public void setRepeat(boolean willRepeat) {
this.willRepeat = willRepeat;
}
private void setup(){
mViewPropertyAnimator.setListener(this);
}
@Override
public void onAnimationStart(Animator animation) {
mCurrentAnimator = animation;
if(mViewPropertyAnimatorListener != null){
mViewPropertyAnimatorListener.onAnimationStart(animation);
}
}
@Override
public void onAnimationEnd(Animator animation) {
if(mViewPropertyAnimatorListener != null){
mViewPropertyAnimatorListener.onAnimationEnd(animation);
}
repeatAnimation();
}
public void start(){
mViewPropertyAnimator.start();
}
private void repeatAnimation() {
if(willRepeat){
start();
onAnimationRepeat(mCurrentAnimator);
}
mInstance = null;
}
@Override
public void onAnimationCancel(Animator animation) {
if(mViewPropertyAnimatorListener != null){
mViewPropertyAnimatorListener.onAnimationCancel(animation);
}
}
@Override
public void onAnimationRepeat(Animator animation) {
if(mViewPropertyAnimatorListener != null){
mViewPropertyAnimatorListener.onAnimationRepeat(animation);
}
}
用法:
RepeatableViewPropertyAnimatorWrapper.wrap(view.animate().alpha(0).setDuration(500)).withAnimatorListener(myListener).start();
请注意,我还没有测试此代码,请记住,根据ViewPropertyAnimator实现,设置值可能会发生变化。