Left to right animated sliding panel?

為{幸葍}努か 提交于 2019-12-08 09:06:52

问题


I'm trying to add a sliding panel to my layout that is similar to sliding drawer, except that it will be placed to the left of my main layout and not overlaying it. There's a small button on the top left of my layout that expands/collapses the panel when I click on it. When it expands/collapses, I want the animation to be smooth so that the view that is adjacent to it will move as well. Here's the code I've tried. The panel stops working after the first expand/collapse:

public Animation expandHiddenPanel(final View v, final boolean expand) {
    panelExpanded = expand;
    v.measure(MeasureSpec.makeMeasureSpec(200, MeasureSpec.AT_MOST), 
              MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));

    final int initialWidth = v.getMeasuredWidth();
    Log.i("test", "initialWidth = " + initialWidth);

    v.getLayoutParams().width = 0;
    v.setVisibility(View.VISIBLE);

    Animation a = new Animation() {
        @Override
        protected void applyTransformation(float interpolatedTime, Transformation t) {
            int newWidth;

            if (expand) {
                newWidth = (int)(initialWidth * interpolatedTime);
                Log.i("test", "new Width = " + newWidth);
            }
            else {
                newWidth = (int)(initialWidth * (1 - interpolatedTime));
                Log.i("test", "new Width = " + newWidth);
            }

            v.getLayoutParams().width = newWidth;
            v.requestLayout();              
        }

        @Override
        public boolean willChangeBounds() {
            return true;
        }
    };

    a.setInterpolator(new AccelerateInterpolator());
    a.setDuration(2500);
    v.startAnimation(a);

    return a;
}

回答1:


Make sure that if u want to show animation in growing width then mention the layout specific width in xml file and use the below code for expand and collapse animation in terms of width.

//expand animation      
public static void expand(final View v) {
        v.measure(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
        final int targtetWidth = v.getMeasuredWidth();
        Log.v("view width", "view expand width==>"+targtetWidth);
        v.getLayoutParams().width = 0;
        v.setVisibility(View.VISIBLE);

        Animation a = new Animation() {
            @Override
            protected void applyTransformation(float interpolatedTime,Transformation t) {
                v.getLayoutParams().width = interpolatedTime == 1 ? LayoutParams.WRAP_CONTENT: (int)(targtetWidth * interpolatedTime);
                v.requestLayout();
            }

            @Override
            public boolean willChangeBounds() {
                return true;
            }
        };

        a.setDuration(100);
        v.startAnimation(a);
    }

//collapse animation
    public static void collapse(final View v) {
        final int initialWidth = v.getMeasuredWidth();
        Log.v("initial width", "initial width==>"+initialWidth); 
        Animation a = new Animation() {
            @Override
            protected void applyTransformation(float interpolatedTime,
                    Transformation t) {
                if (interpolatedTime == 1) {
                    Log.v("interpolated", "interpolated time is 1");
                    v.setVisibility(View.GONE);
                } else {

                    v.getLayoutParams().width = initialWidth - (int) (initialWidth * interpolatedTime);
                    Log.v("interpolated", "interpolated time is===>"+v.getLayoutParams().width);
                    v.requestLayout();
                }
            }

            @Override
            public boolean willChangeBounds() {
                return true;
            }
        };

        // 1dp/ms
        Log.v("duration", "duration for collapse==>"+((int)(initialWidth /v.getContext().getResources().getDisplayMetrics().density)));
        a.setDuration((int) (initialWidth / v.getContext().getResources().getDisplayMetrics().density));
        v.startAnimation(a);
    }


来源:https://stackoverflow.com/questions/6638566/left-to-right-animated-sliding-panel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!