valueanimator.start在API<上的valueanimator.cancel之后重新开始动画。 19

时间:2017-06-18 09:30:40

标签: android objectanimator

我知道在我尝试所有解决方案之前会问这个问题,但是我无法解决这个问题, 我有使用android库中的翻译从屏幕顶部到屏幕底部的对象。

我想暂停动画并恢复它:

PAUSE CODE修复了其中一个堆栈溢出帖子的建议:

private void pauseAnimation() {
        p = true;
        Pausearray = new MyClass[mAllImageViews.size()];
        int count = 0;
        for (ArrowView v : mAllImageViews) {
            ValueAnimator va = (ValueAnimator) v.getTag();
            if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                va.pause();
            } else {
                if (va != null) {
                    MyClass temp = new MyClass();
                    temp.tag = v.getTag().toString();
                    temp.playtime = va.getCurrentPlayTime();
                    Pausearray[count] = temp;
                    va.cancel();
                }
            }
            count ++;
        }
    }

暂停工作正在停止移动并停留在其位置。

其中一个堆栈溢出帖子建议的RESUME CODE:

   private void resumeAnimation() {
        p = false;
        if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            for (ArrowView v : mAllImageViews) {
                try {
                    ValueAnimator va = (ValueAnimator) v.getTag();
                    va.resume();
                } catch (Exception ex) {

                }
            }
        } else {
            if(Pausearray!=null) {
                for (ArrowView v : mAllImageViews) {
                    ValueAnimator va = (ValueAnimator) v.getTag();
                    for (int i = 0; i < Pausearray.length; i++) {
                        if(v.getTag().toString().equalsIgnoreCase(Pausearray[i].tag)){
                            va.start();
                            va.setCurrentPlayTime(Pausearray[i].playtime);
                        }
                    }
                }
            }
        }
    }

简历的问题是它再次从顶部开始我想让它继续它在屏幕上的位置。谢谢!

更新

我试图从屏幕上获取箭头视图位置并将其设置为恢复,我仍然遇到相同的问题,视图正在重新启动,因此编辑的代码是:

  private void resumeAnimation() {
        p = false;
        if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            for (ArrowView v : mAllImageViews) {
                try {
                    ValueAnimator va = (ValueAnimator) v.getTag();
                    va.resume();
                } catch (Exception ex) {

                }
            }
        } else {
            if(Pausearray!=null) {
                for (ArrowView v : mAllImageViews) {
                    ValueAnimator va = (ValueAnimator) v.getTag();
                    for (int i = 0; i < Pausearray.length; i++) {
                        if(v.getTag().toString().equalsIgnoreCase(Pausearray[i].tag)){
                            Log.w("PAUSETEST","Setting Parameters "+Pausearray[i].playtime);
                            va.start();
                            va.setCurrentPlayTime(Pausearray[i].playtime);
                            v.setY(Pausearray[i].translationy); // i took the location via v.gettranslationY on pause
                        }
                    }
                }
            }
        }
    }

更新: 我在搜索时听到,valueanimator没有重启,所以我将valueanimator切换为对象动画师动画代码:

 public void startAnimation(final ArrowView aniView) {
    aniView.setPivotX(aniView.getWidth());
    aniView.setPivotY(aniView.getHeight());

    long delay = new Random().nextInt(Constants.MAX_DELAY);// generate a random delay time before the animation starts to go down

    ObjectAnimator animator = ObjectAnimator.ofFloat(aniView,"translationY", mDisplaySize.bottom - (buttonsize) - (90 * mScale));
    if (aniView.getColor() == 0 && aniView.draw == false) {
        Constants.ANIM_DURATION = Constants.ANIM_DURATION_NORMAL;
    } else if (aniView.getColor() != 0 && aniView.draw == false) {
        Constants.ANIM_DURATION = Constants.ANIM_DURATION_COLOR;
    } else if (aniView.getColor() == 0 && aniView.draw == true) {
        Constants.ANIM_DURATION = Constants.ANIM_DURATION_COLOR;
    } else if (aniView.getColor() != 0 && aniView.draw == true) {
        Constants.ANIM_DURATION = Constants.ANIM_DURATION_COLORANDCALC;
    }
    animator.setDuration(Constants.ANIM_DURATION);// speed of the falldown
    animator.setInterpolator(new AccelerateInterpolator());
    animator.setStartDelay(delay);// start the animation after the delay.
//        animator.addUpdateListener(new AnimatorUpdateListener() {
//
//            //int angle = 50 + (int) (Math.random() * 101);
//            //int movex = new Random().nextInt(mDisplaySize.right);
//
//            @Override
//            public void onAnimationUpdate(ValueAnimator animation) {
//                float value = ((Float) (animation.getAnimatedValue())).floatValue();
//                //aniView.setRotation(angle*value);
//                //aniView.setTranslationX((movex-40)*value);
//                aniView.setTranslationY((mDisplaySize.bottom - (buttonsize)) * value - (90 * mScale));// set the translation to fall down and stop on the top of buttons
//            }
//        });
    aniView.setTag(animator);
    animator.start();
}

我更改了暂停/恢复的代码,将其转换为对象动画师,我仍然面临同样的问题......开始时重启动画!请有人帮忙!谢谢!

1 个答案:

答案 0 :(得分:0)

玩了一下valueanimator类后,我找到了一个让动画停止的技巧,并恢复它不是一种真实的方式,但它解决了这个问题,我从值动画师类扩展并取代了我的主类中的普通动画师myvalueanimator类,以及价值动画师类中的游戏:

import android.animation.ValueAnimator;
import android.util.Log;

import java.util.Timer;

/**
 * Created by win7 on 6/21/2017.
 */

public class MyValueAnimator extends ValueAnimator {
    private float animatedValue = 0;
    private long currenttime;
    private long totaltime;
    private boolean onetime = true;
    private volatile boolean mPaused = false;
    private Timer t;

public void pause() {
    animatedValue = (float) getAnimatedValue();
    mPaused = true;
}

//    @Override
//    public void setValues(PropertyValuesHolder... values) {
//        if (mPaused)
//            return;
//        super.setValues(values);
@Override
public Object getAnimatedValue() {
    if (mPaused) {
        if(onetime){
            currenttime = getCurrentPlayTime();
         //   totaltime = getStartDelay()+ (getDuration() * (getRepeatCount() + 1));
            totaltime = getDuration();
            setDuration(Long.parseLong("9999999999999999"));
            Log.w("PauseDur",Long.parseLong("9999999999999999")+"");
            onetime =false;
        }
        return animatedValue;
    }
    return super.getAnimatedValue();
}

public void resume() {
    mPaused = false;
    onetime=true;
    setCurrentPlayTime(currenttime);
    setDuration(totaltime);
}

public MyValueAnimator(float from, float to) {
    setFloatValues(from, to);
}

}