动画后按钮消失

时间:2016-08-30 08:37:15

标签: android animation

我有一个触摸时动画的按钮。动画正在运行,但在动画完成后,我调用setAnimation(null)重置按钮,但按钮没有回到默认位置。

这是代码:

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 个答案:

答案 0 :(得分:1)

请试试这个:

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) {

        }
    });

希望这会对你有所帮助。

答案 1 :(得分:0)

如果您想重置位置,请尝试Animation.reset();

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