Android: show/hide a view using an animation

后端 未结 9 2274
青春惊慌失措
青春惊慌失措 2021-01-30 00:53

I\'ve been looking through many google results / questions on here to determine how to show/hide a view via a vertical animation, but I can\'t seem to find one that\'s exactly r

9条回答
  •  情书的邮戳
    2021-01-30 01:08

    First of all get the height of the view yo want to saw and make a boolean to save if the view is showing:

    int heigth=0;
    boolean showing=false;
    LinearLayout layout = (LinearLayout) view.findViewById(R.id.layout);
    
            proDetailsLL.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
    
                @Override
                public void onGlobalLayout() {
                    // gets called after layout has been done but before display
                    // so we can get the height then hide the view
    
                    proHeight = proDetailsLL.getHeight(); // Ahaha!  Gotcha
    
                    proDetailsLL.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                    proDetailsLL.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, 0));
                }
            });
    

    Then call the method for showing hide the view, and change the value of the boolean:

    slideInOutAnimation(showing, heigth, layout);
    proShowing = !proShowing;
    

    The method:

    /**
         * Method to slide in out the layout
         * 
         * @param isShowing
         *            if the layout is showing
         * @param height
         *            the height to slide
         * @param slideLL
         *            the container to show
         */
    private void slideInOutAnimation(boolean isShowing, int height, final LinearLayout slideLL, final ImageView arroIV) {
    
            if (!isShowing) {
            Animation animIn = new Animation() {
            protected void applyTransformation(float interpolatedTime, Transformation t) {
                        super.applyTransformation(interpolatedTime, t);
            // Do relevant calculations here using the interpolatedTime that runs from 0 to 1
            slideLL.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, (int) (heigth * interpolatedTime)));
    
                    }
                };
                animIn.setDuration(500);
                slideLL.startAnimation(animIn);
            } else {
    
                Animation animOut = new Animation() {
                    protected void applyTransformation(float interpolatedTime, Transformation t) {
                        super.applyTransformation(interpolatedTime, t);
                        // Do relevant calculations here using the interpolatedTime that runs from 0 to 1
    
    
                            slideLL.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,
                                    (int) (heigth * (1 - interpolatedTime))));
    
                    }
                };
                animOut.setDuration(500);
                slideLL.startAnimation(animOut);
    
    
            }
    
        }
    

提交回复
热议问题