从左向右滑动动画面板? [英] Left to right animated sliding panel?

查看:181
本文介绍了从左向右滑动动画面板?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想滑动面板添加到我的布局类似于滑动抽屉,但它会放在我的主要布局的左边,而不是覆盖它。有一个关于我的布局左上方的展开/折叠面板,当我点击它一个小按钮。当它展开/折叠,我希望动画流畅,使邻近于它的视图将移动为好。这里的code我试过。该小组停止第一展开/折叠后的工作:

 动漫公共expandHiddenPanel(最终视图V,最终布尔扩大){
    panelExpanded =扩大;
    v.measure(MeasureSpec.makeMeasureSpec(200,MeasureSpec.AT_MOST)
              MeasureSpec.makeMeasureSpec(0,MeasureSpec.UNSPECIFIED));    最终诠释initialWidth = v.getMeasuredWidth();
    Log.i(测试,initialWidth =+ initialWidth);    。v.getLayoutParams()宽度= 0;
    v.setVisibility(View.VISIBLE);    动画=新的动画(){
        @覆盖
        保护无效applyTransformation(浮动interpolatedTime,变换T){
            INT newWidth;            如果(扩大){
                newWidth =(INT)(initialWidth * interpolatedTime);
                Log.i(测试,新宽度=+ newWidth);
            }
            其他{
                newWidth =(INT)(initialWidth *(1 - interpolatedTime));
                Log.i(测试,新宽度=+ newWidth);
            }            v.getLayoutParams()宽= newWidth。
            v.requestLayout();
        }        @覆盖
        公共布尔willChangeBounds(){
            返回true;
        }
    };    a.setInterpolator(新AccelerateInterpolator());
    a.setDuration(2500);
    v.startAnimation(一);    返回;
}


解决方案

确认,如果ü要表现出越来越宽的动画则提到在XML文件中的布局特定的宽度,并使用下面code的展开和折叠动画在宽度方面。

  //拓展动画
公共静态无效的扩大(最终查看V){
        v.measure(LayoutParams.WRAP_CONTENT,LayoutParams.MATCH_PARENT);
        最终诠释targtetWidth = v.getMeasuredWidth();
        Log.v(查看宽度,视图中展开宽度==>中+ targtetWidth);
        。v.getLayoutParams()宽度= 0;
        v.setVisibility(View.VISIBLE);        动画=新的动画(){
            @覆盖
            保护无效applyTransformation(浮动interpolatedTime,变换T){
                v.getLayoutParams()。WIDTH = interpolatedTime == 1? LayoutParams.WRAP_CONTENT:(INT)(targtetWidth * interpolatedTime);
                v.requestLayout();
            }            @覆盖
            公共布尔willChangeBounds(){
                返回true;
            }
        };        a.setDuration(100);
        v.startAnimation(一);
    }//合拢动画
    公共静态无效塌陷(最终查看V){
        最终诠释initialWidth = v.getMeasuredWidth();
        Log.v(初始宽度,初始宽度==>中+ initialWidth);
        动画=新的动画(){
            @覆盖
            保护无效applyTransformation(浮动interpolatedTime,
                    变换T){
                如果(interpolatedTime == 1){
                    Log.v(内插,内插的时间为1);
                    v.setVisibility(View.GONE);
                }其他{                    。v.getLayoutParams()宽= initialWidth - (INT)(initialWidth * interpolatedTime);
                    Log.v(内插,内插的时间是===>中+ v.getLayoutParams()的宽度。);
                    v.requestLayout();
                }
            }            @覆盖
            公共布尔willChangeBounds(){
                返回true;
            }
        };        // 1DP / MS
        Log.v(时间,持续时间崩溃==>中+((int)的(initialWidth /v.getContext().getResources().getDisplayMetrics().density)));
        a.setDuration(。(INT)(initialWidth / v.getContext()getResources()getDisplayMetrics()密度));
        v.startAnimation(一);
    }

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;
}

解决方案

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);
    }

这篇关于从左向右滑动动画面板?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆