Button Disappears After Animation

2019-09-11 05:48发布

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

2条回答
唯我独甜
2楼-- · 2019-09-11 05:54

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

animate.setAnimationListener(new AnimationListener() {
    @Override
    public void onAnimationEnd(Animation animation)
    {
        animation.reset();
    }
}
查看更多
女痞
3楼-- · 2019-09-11 06:04

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.

查看更多
登录 后发表回答