Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/179.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在android中为CardView(即SlideUp和SlideDown)中的布局设置动画_Android_Android Animation_Material Design - Fatal编程技术网

如何在android中为CardView(即SlideUp和SlideDown)中的布局设置动画

如何在android中为CardView(即SlideUp和SlideDown)中的布局设置动画,android,android-animation,material-design,Android,Android Animation,Material Design,我有一个包含3个动作按钮的相对可视性。我想动画这个布局,以SlideUp和SlideDown点击下拉图像 下面是一个实用程序类,可以为您执行此操作: public class AnimExpandCollapse { public static void expand(final View v) { v.measure( LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT ); final int targetHeight

我有一个包含3个动作按钮的相对可视性。我想动画这个布局,以SlideUp和SlideDown点击下拉图像


下面是一个实用程序类,可以为您执行此操作:

public class AnimExpandCollapse {

public static void expand(final View v) {
    v.measure( LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT );
    final int targetHeight = v.getMeasuredHeight();

    v.getLayoutParams().height = 0;
    v.setVisibility(View.VISIBLE);
    Animation a = new Animation()
    {
        @Override
        protected void applyTransformation(float interpolatedTime, Transformation t) {
            v.getLayoutParams().height = interpolatedTime == 1
                    ? LayoutParams.WRAP_CONTENT
                    : (int)(targetHeight * interpolatedTime);
            v.requestLayout();
        }
        @Override
        public boolean willChangeBounds() {
            return true;
        }
    };

    // 1dp/ms
    a.setDuration((int)(targetHeight / v.getContext().getResources().getDisplayMetrics().density));
    v.startAnimation(a);
}

public static void setViewToWrappedHeight(View v) {
    v.getLayoutParams().height = LayoutParams.WRAP_CONTENT;
}

public static void collapse(final View v) {
    collapse(v, null, true, 0);
}

public static void collapse(final View v, Animation.AnimationListener animationListener, boolean autoDuration, int duration) {

    final int initialHeight = v.getMeasuredHeight();

    Animation a = new Animation()
    {
        @Override
        protected void applyTransformation(float interpolatedTime, Transformation t) {
            if (interpolatedTime == 1){
                v.setVisibility(View.GONE);
            } else {
                v.getLayoutParams().height = initialHeight - (int)(initialHeight * interpolatedTime);
                v.requestLayout();
            }
        }

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

    // 1dp/ms
    if (autoDuration) {
        a.setDuration((int)(initialHeight / v.getContext().getResources().getDisplayMetrics().density));
    } else {
        a.setDuration(duration);
    }
    if (animationListener!=null) a.setAnimationListener(animationListener);
    v.startAnimation(a);
}}