Hide view with animation and set visibility to gon

2019-04-06 22:24发布

问题:

i want to hide dicelayout (dicelout was in mainboardlinear) but when animation finished the screen flashed because of View.GONE! if i set setfillafter to true and clear View.GONE, i do not have the flash problem anymore but my scrollview in mainboardlinear can't be scrolled in this case!

                final RelativeLayout rLayout=(RelativeLayout)findViewById(R.id.dicelayout);


            Animation animation=new TranslateAnimation(0, 0, 0, -rLayout.getHeight());
            animation.setFillAfter(false);
            animation.setFillBefore(true);
            animation.setDuration(1000);
            ((LinearLayout)findViewById(R.id.mainboardlinear)).startAnimation(animation);


            Thread t=new Thread(new Runnable() {
                public void run() {
                try {
                    Thread.sleep(999);
                    runOnUiThread(new Runnable() {
                        public void run() {

                        rLayout.setVisibility(View.GONE);
                        }
                    });

                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }   
                }
            });
            t.start();

回答1:

using onAnimationEnd from setAnimationListener and setFillEnabled solve my problem.

Animation animation=new TranslateAnimation(0, 0, 0, -rLayout.getHeight());
            animation.setFillEnabled(true);

            animation.setDuration(1000);
            animation.setAnimationListener(new AnimationListener() {

                @Override
                public void onAnimationStart(Animation animation) {
                    // TODO Auto-generated method stub

                }

                @Override
                public void onAnimationRepeat(Animation animation) {
                    // TODO Auto-generated method stub

                }

                @Override
                public void onAnimationEnd(Animation animatiofillAftern) {
                    // TODO Auto-generated method stub
                    //                      mainBoardLinear.removeView(rLayout);
                    rLayout.setVisibility(View.GONE);

                }
            });

            mainBoardLinear.startAnimation(animation);