Android视图扩展了动画

时间:2013-11-08 16:02:20

标签: android android-layout onclick android-animation

我正在尝试编辑these source以创建一个适用于我所有观看次数的简单函数。

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

RelativeLayout mLayout1 = (RelativeLayout) findViewById(R.id.layout1);
RelativeLayout mLayoutToBeExpanded1 = (RelativeLayout) findViewById(R.id.layout_exp_1);
addAction(mLayout, mLayoutToBeExpanded);

RelativeLayout mLayout2 = (RelativeLayout) findViewById(R.id.layout2);
RelativeLayout mLayoutToBeExpanded2 = (RelativeLayout) findViewById(R.id.layout_exp_2);
addAction(mLayout2, mLayoutToBeExpanded2);

RelativeLayout mLayout3 = (RelativeLayout) findViewById(R.id.layout3);
RelativeLayout mLayoutToBeExpanded3 = (RelativeLayout) findViewById(R.id.layout_exp_3);
addAction(mLayout3, mLayoutToBeExpanded3);

}

    public void addAction(final View layout, final View summary) {

    summary.getViewTreeObserver().addOnPreDrawListener(
            new ViewTreeObserver.OnPreDrawListener() {

                @Override
                public boolean onPreDraw() {
                    summary.getViewTreeObserver().removeOnPreDrawListener(this);
                    summary.setVisibility(View.GONE);

                    final int widthSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
                    final int heightSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
                    summary.measure(widthSpec, heightSpec);

                    mAnimator = slideAnimator(0, summary.getMeasuredHeight(), summary);
                    return true;
                }
            });

    layout.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            if (summary.getVisibility() == View.GONE) {
                expand(summary);
            } else {
                collapse(summary);
            }
        }
    });
}

private void expand(View summary) {
    //set Visible
    summary.setVisibility(View.VISIBLE);

            /* Remove and used in preDrawListener
            final int widthSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
            final int heightSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
            mLinearLayout.measure(widthSpec, heightSpec);

            mAnimator = slideAnimator(0, mLinearLayout.getMeasuredHeight());
            */

    mAnimator.start();
}

private void collapse(final View summary) {
    int finalHeight = summary.getHeight();

    ValueAnimator mAnimator = slideAnimator(finalHeight, 0, summary);

    mAnimator.addListener(new Animator.AnimatorListener() {
        @Override
        public void onAnimationEnd(Animator animator) {
            //Height=0, but it set visibility to GONE
            summary.setVisibility(View.GONE);
        }

        @Override
        public void onAnimationStart(Animator animator) {
        }

        @Override
        public void onAnimationCancel(Animator animator) {
        }

        @Override
        public void onAnimationRepeat(Animator animator) {
        }
    });
    mAnimator.start();
}


private ValueAnimator slideAnimator(int start, int end, final View summary) {

    ValueAnimator animator = ValueAnimator.ofInt(start, end);


    animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
        @Override
        public void onAnimationUpdate(ValueAnimator valueAnimator) {
            //Update Height
            int value = (Integer) valueAnimator.getAnimatedValue();

            ViewGroup.LayoutParams layoutParams = summary.getLayoutParams();
            layoutParams.height = value;
            summary.setLayoutParams(layoutParams);
        }
    });
    return animator;
}

问题:前2个布局只能工作一次。第一次扩展后 - >崩溃onClickFunction无法正常工作。第3个布局工作正常,我可以多次展开/折叠它。任何解决方案?

4 个答案:

答案 0 :(得分:17)

我自己解决了,这是可扩展布局的工作解决方案。

使用:

addAction(YourLayout, LayoutToBeExpanded)

功能:

    public void addAction(final View layout, final View summary) {



    layout.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            if (summary.getVisibility() == View.GONE) {
                expand(summary);
            } else {
                collapse(summary);
            }
        }
    });
}

private void expand(View summary) {
    //set Visible
    summary.setVisibility(View.VISIBLE);

            final int widthSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
            summary.measure(widthSpec, 300);

            mAnimator = slideAnimator(0, 300, summary);

    mAnimator.start();
}

private void collapse(final View summary) {
    int finalHeight = summary.getHeight();

    ValueAnimator mAnimator = slideAnimator(finalHeight, 0, summary);

    mAnimator.addListener(new Animator.AnimatorListener() {
        @Override
        public void onAnimationEnd(Animator animator) {
            //Height=0, but it set visibility to GONE
            summary.setVisibility(View.GONE);
        }

        @Override
        public void onAnimationStart(Animator animator) {
        }

        @Override
        public void onAnimationCancel(Animator animator) {
        }

        @Override
        public void onAnimationRepeat(Animator animator) {
        }
    });
    mAnimator.start();
}


private ValueAnimator slideAnimator(int start, int end, final View summary) {

    ValueAnimator animator = ValueAnimator.ofInt(start, end);


    animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
        @Override
        public void onAnimationUpdate(ValueAnimator valueAnimator) {
            //Update Height
            int value = (Integer) valueAnimator.getAnimatedValue();

            ViewGroup.LayoutParams layoutParams = summary.getLayoutParams();
            layoutParams.height = value;
            summary.setLayoutParams(layoutParams);
        }
    });
    return animator;
}

如果你想在自助点击时崩溃“mLayoutToBeExpanded”,请使用:

    public void addAction(final View layout, final View summary) {
    layout.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (summary.getVisibility() == View.GONE) {
                expand(summary);
            } else {
                collapse(summary);
            }
        }
    });

    summary.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (summary.getVisibility() == View.VISIBLE) {
                collapse(summary);
            } else {
                //nothing
            }
        }
    });
}

答案 1 :(得分:2)

使用此方法实现折叠并展开动画:

       public void expandOrCollapse(final View v,String exp_or_colpse) {
    TranslateAnimation anim = null;
    if(exp_or_colpse.equals("expand"))
    {
        anim = new TranslateAnimation(0.0f, 0.0f, -v.getHeight(), 0.0f);
        v.setVisibility(View.VISIBLE);  
    }
    else{
        anim = new TranslateAnimation(0.0f, 0.0f, 0.0f, -v.getHeight());
        AnimationListener collapselistener= new AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
            }

            @Override
            public void onAnimationRepeat(Animation animation) {
            }

            @Override
            public void onAnimationEnd(Animation animation) {
            v.setVisibility(View.GONE);
            }
        };

        anim.setAnimationListener(collapselistener);
    }

     // To Collapse
        //

    anim.setDuration(300);
    anim.setInterpolator(new AccelerateInterpolator(0.5f));
    v.startAnimation(anim);
}

并在您的代码中调用此方法:

        //For Expand View
       expandOrCollapse(YourView,"expand");

      //For Collapse View
       expandOrCollapse(yourView,"collapse");

答案 2 :(得分:0)

    ValueAnimator expandAnimation = ValueAnimator.ofInt(mainView.getHeight(), 400);
    expandAnimation.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
        @Override
        public void onAnimationUpdate(final ValueAnimator animation) {
            int height = (Integer) animation.getAnimatedValue();
            RelativeLayout.LayoutParams lp = (LayoutParams) mainView.getLayoutParams();
            lp.height = height;
        }
    });


    expandAnimation.setDuration(500);
    expandAnimation.start();

答案 3 :(得分:0)

只是一种替代方案,易于使用。 要使用动画展开视图:

view.setPivotY(0);
ObjectAnimator.ofFloat(view, "scaleY", 0f, 1f).setDuration(300).start();