How to remove a View when animation ends?

后端 未结 2 1475
一整个雨季
一整个雨季 2020-12-05 10:56

I\'m creating a game and I would like to display a simple \"score\"-animation to the player when credits are given to him. This is the view I throw onto the screen:

相关标签:
2条回答
  • 2020-12-05 11:20

    I also found that when removing a view from its parent after applying an animation to this view (using onAnimationEnd) crashes with NPE on the dispatchDraw of the parent.

    The only solution I found is to trigger the removal inside a post call. Normally all UI modification must be done on the UI thread, so I added a runOnUiThread call on the activity, but it could be useless (it works for me without that).

    Animation animation = AnimationUtils.loadAnimation(parentView.getContext(), animationId);
    animation.setAnimationListener(new AnimationListener() {
        public void onAnimationStart(Animation paramAnimation) { }
        public void onAnimationRepeat(Animation paramAnimation) { }
        public void onAnimationEnd(Animation paramAnimation) { 
            // without the post method, the main UI crashes if the view is removed 
            parentView.post(new Runnable() {
                public void run() {
                    // it works without the runOnUiThread, but all UI updates must 
                    // be done on the UI thread
                    activity.runOnUiThread(new Runnable() {
                        public void run() {
                            parentView.removeView(view);
                        }
                    });
                }
            });
        }
    });
    
    view.setVisibility(visibility());
    view.startAnimation(animation);
    
    0 讨论(0)
  • 2020-12-05 11:22

    When using AnimationUtils.loadAnimation view.clearAnimation() solved my problem

    0 讨论(0)
提交回复
热议问题