Value/Object Animantor API 19 < on resume restart the animation

  • Replies:0
  • OpenNot stickiedUnanswered
  • Forum posts: 1

Jun 19, 2017 2:52:16 PM via Website

Hello Guys, i'm new here it seems that it's a nice forum! anyway i'm seeking help i hope that you can help me!

i want to the pause the animation and resume it:

PAUSE CODE fixed from the suggestion on one of the stack overflow posts:

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 ++;
        }
    }

the pause is working the annimation is stoping from moving and its staying in its position.

RESUME CODE suggested by one of the stack overflow posts:

  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);
                        }
                    }
                }
            }
        }
    }

the problem with the resume that it's starting from the top again i want it to continue where it was it's position on screen. Thank you!

i tried to get the arrow view position from screen and set it at resume, i'm still getting the same issue, views are restarting so the edited code is:

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
                    }
                }
            }
        }
    }
}

i heard while i was searching that valueanimator doesn't restart so i switched the valueanimator to object animator animator code:

 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();
}

and i changed the code of the pause/resume to cast it into object animator and I'm still facing the same problem... on start is restarting the animation! please someone help! thanks!

— modified on Jun 19, 2017 2:53:27 PM