从左到右动画滑动面板?

问题描述 投票:0回答:1

我正在尝试在我的布局中添加一个滑动面板,类似于滑动抽屉,除了它将放置在我的主布局的左侧而不是覆盖它。我的布局左上角有一个小按钮,当我点击它时会扩展/折叠面板。当它展开/折叠时,我希望动画是平滑的,这样与它相邻的视图也会移动。这是我尝试过的代码。第一次展开/折叠后面板停止工作:

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;
}
android animation expand collapse
1个回答
0
投票

确保如果您想以增长的宽度显示动画,请在xml文件中提及特定于布局的宽度,并使用以下代码以宽度方式展开和折叠动画。

//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);
    }
© www.soinside.com 2019 - 2024. All rights reserved.