Button Disappears After Animation

2019-09-11 05:41发布

问题:

i have a button that i animating when touched. the animation is working but after the animation is compelete, i am calling setAnimation(null) to reset the button, but the button is not getting back to its default position.

here is the code:

ok_btn_id.setOnTouchListener(new OnSwipeTouchListener(BarCodeReaderActivity.this) {
            public void onSwipeTop() {
            }
            public void onSwipeRight() {

                slidefromLeftToRight(ok_btn_id);
            }
            public void onSwipeLeft() {
                slidefromRightToLeft(ok_btn_id);
            }
            public void onSwipeBottom() {
            }
        });

public void slidefromRightToLeft(final View view)
    {
        TranslateAnimation animate;

        DisplayMetrics metrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(metrics);

        if (view.getHeight() == 0) {
            animate = new TranslateAnimation(metrics.widthPixels/2,
                    0, 0, 0);
        } else {
            animate = new TranslateAnimation(0,-viewGroup.getWidth(), 0, 0); // View for animation
        }

        animate.setDuration(500);
        animate.setFillAfter(false);

        view.startAnimation(animate);
        view.postDelayed(new Runnable() {
            @Override
            public void run() {
               view.setAnimation(null);
                view.setVisibility(View.VISIBLE); 
            }
        }, 500);
    }

回答1:

Please try this:

animation.setAnimationListener(new Animation.AnimationListener() {
        @Override
        public void onAnimationStart(Animation animation) {
            aView.setVisibility(View.VISIBLE);
        }

        @Override
        public void onAnimationEnd(Animation animation) {

            button.clearAnimation();

        }

        @Override
        public void onAnimationRepeat(Animation animation) {

        }
    });

Hope this will help you.



回答2:

If you want to reset the position try Animation.reset();

animate.setAnimationListener(new AnimationListener() {
    @Override
    public void onAnimationEnd(Animation animation)
    {
        animation.reset();
    }
}