如何在android中为CardView(即SlideUp和SlideDown)中的布局设置动画
我有一个包含3个动作按钮的相对可视性。我想动画这个布局,以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
下面是一个实用程序类,可以为您执行此操作:
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);
}}