How to stop an animation (cancel() does not work)

后端 未结 7 1020
轮回少年
轮回少年 2020-11-29 16:52

I need to stop a running translate animation. The .cancel() method of Animation has no effect; the animation goes until the end anyway.

How

相关标签:
7条回答
  • 2020-11-29 17:15

    You must use .clearAnimation(); method in UI thread:

    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            v.clearAnimation();
        }
    });
    
    0 讨论(0)
  • 2020-11-29 17:15

    To stop animation you may set such objectAnimator that do nothing, e.g.

    first when manual flipping there is animation left to right:

    flipper.setInAnimation(leftIn);
    flipper.setOutAnimation(rightOut);
    

    then when switching to auto flipping there's no animation

    flipper.setInAnimation(doNothing);
    flipper.setOutAnimation(doNothing);
    
    doNothing = ObjectAnimator.ofFloat(flipper, "x", 0f, 0f).setDuration(flipperSwipingDuration);
    
    0 讨论(0)
  • 2020-11-29 17:22

    What you can try to do is get the transformation Matrix from the animation before you stop it and inspect the Matrix contents to get the position values you are looking for.

    Here are the api's you should look into

    public boolean getTransformation (long currentTime, Transformation outTransformation)

    public Matrix getMatrix ()

    public void getValues (float[] values)

    So for example (some pseudo code. I have not tested this):

    Transformation outTransformation = new Transformation();
    myAnimation.getTransformation(currentTime, outTransformation);
    Matrix transformationMatrix = outTransformation.getMatrix();
    float[] matrixValues = new float[9];
    transformationMatrix.getValues(matrixValues);
    float transX = matrixValues[Matrix.MTRANS_X];
    float transY = matrixValues[Matrix.MTRANS_Y];
    
    0 讨论(0)
  • 2020-11-29 17:28

    On Android 4.4.4, it seems the only way I could stop an alpha fading animation on a View was calling View.animate().cancel() (i.e., calling .cancel() on the View's ViewPropertyAnimator).

    Here's the code I'm using for compatibility before and after ICS:

    public void stopAnimation(View v) {
        v.clearAnimation();
        if (canCancelAnimation()) {
            v.animate().cancel();
        }
    }
    

    ... with the method:

    /**
     * Returns true if the API level supports canceling existing animations via the
     * ViewPropertyAnimator, and false if it does not
     * @return true if the API level supports canceling existing animations via the
     * ViewPropertyAnimator, and false if it does not
     */
    public static boolean canCancelAnimation() {
        return Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH;
    }
    

    Here's the animation that I'm stopping:

    v.setAlpha(0f);
    v.setVisibility(View.VISIBLE);
    // Animate the content view to 100% opacity, and clear any animation listener set on the view.
    v.animate()
        .alpha(1f)
        .setDuration(animationDuration)
        .setListener(null);
    
    0 讨论(0)
  • 2020-11-29 17:34

    Call clearAnimation() on whichever View you called startAnimation().

    0 讨论(0)
  • 2020-11-29 17:34

    If you are using the animation listener, set v.setAnimationListener(null). Use the following code with all options.

    v.getAnimation().cancel();
    v.clearAnimation();
    animation.setAnimationListener(null);
    
    0 讨论(0)
提交回复
热议问题