Android 如何将高度布局更改为动画(以编程方式)

Android 如何将高度布局更改为动画(以编程方式),android,android-animation,Android,Android Animation,如何以动画形式编程更改高度布局 第一: 之后: 嘿,朋友 在测试代码之后,我发现了一个小问题。因为我使用了“scaleY”它只是“拉伸”了视图。这意味着,如果视图中有一些文本或其他内容,它只会拉伸它,看起来不好看。尝试使用ValueAnimator取而代之,它的工作更平滑 public void onClick(View v) { if(!isBig){ ValueAnimator va = ValueAnimator.ofInt(100, 200);

如何以动画形式编程更改高度布局

第一:

之后:

嘿,朋友

在测试代码之后,我发现了一个小问题。因为我使用了
“scaleY”
它只是“拉伸”了视图。这意味着,如果视图中有一些文本或其他内容,它只会拉伸它,看起来不好看。尝试使用
ValueAnimator
取而代之,它的工作更平滑

public void onClick(View v)
{
    if(!isBig){
        ValueAnimator va = ValueAnimator.ofInt(100, 200);
        va.setDuration(400);
        va.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            public void onAnimationUpdate(ValueAnimator animation) {
                Integer value = (Integer) animation.getAnimatedValue();
                v.getLayoutParams().height = value.intValue();
                v.requestLayout();
            }
        });
        va.start();
        isBig = true;
    }
    else{
        ValueAnimator va = ValueAnimator.ofInt(200, 100);
        va.setDuration(400);
        va.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            public void onAnimationUpdate(ValueAnimator animation) {
                Integer value = (Integer) animation.getAnimatedValue();
                v.getLayoutParams().height = value.intValue();
                v.requestLayout();
            }
        });
        va.start();
        isBig = false;
    }
}
XML:

<RelativeLayout
    android:layout_width="150dp"
    android:layout_height="100dp"
    android:layout_centerHorizontal="true"
    android:background="@android:color/holo_red_dark"
    android:onClick="onButtonClick"
    android:clickable="true">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="My Layout"/>
</RelativeLayout>

我已经更新了答案。使用
ValueAnimator
,如果视图/布局中有一些文本或其他内容,则效果会更好
private boolean isBig = false;

...

public void onClick(View v)
{
    v.setPivotY(0f);
    if(!isBig){
        ObjectAnimator scaleY = ObjectAnimator.ofFloat(v, "scaleY", 2f);
        scaleY.setInterpolator(new DecelerateInterpolator());
        scaleY.start();
        isBig = true;
    }
    else{
        ObjectAnimator scaleY = ObjectAnimator.ofFloat(v, "scaleY", 1f);
        scaleY.setInterpolator(new DecelerateInterpolator());
        scaleY.start();
        isBig = false;
    }
}