【问题标题】:Left to right animated sliding panel?从左到右的动画滑动面板?
【发布时间】:2011-10-02 02:04:54
【问题描述】:

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

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】:

    确保如果你想以增长的宽度显示动画,然后在 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);
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-07-06
      • 2021-07-05
      • 1970-01-01
      • 2015-07-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多