动画改变 LayoutParams (RelativeLayout)

Animate changing LayoutParams (RelativeLayout)

当我单击 RelativeLayout 时,它会更改大小。 我怎样才能使这个变化动画化?所以它看起来像矩形增长到全屏?

我的布局:

<RelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" 
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" 
tools:context=".MainActivity"
android:background="#ff000000">

<RelativeLayout
    android:id="@+id/srLayout"
    android:layout_width="62.5dp"
    android:layout_height="132.5dp"
    android:layout_alignParentTop="true"
    android:layout_alignParentEnd="false"
    android:layout_alignParentRight="true"
    android:layout_marginRight="10dp"
    android:layout_marginEnd="10dp"
    android:layout_marginTop="10dp"
    android:clickable="true"
    android:onClick="sleepRoom"
    android:background="#ffffffff"></RelativeLayout>

</RelativeLayout>

代码:

public void sleepRoom(View view) {
    RelativeLayout sr = (RelativeLayout) findViewById(view.getId());
    RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) sr.getLayoutParams();
    params.height = RelativeLayout.LayoutParams.MATCH_PARENT;
    params.width = RelativeLayout.LayoutParams.MATCH_PARENT;
    sr.setLayoutParams(params);
}

要在这种情况下明确设置动画,您的 sleepRoom() 应如下所示。

public void sleepRoom(View view) {
    final RelativeLayout sr = (RelativeLayout) view;
    RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) sr.getLayoutParams();
    params.height = RelativeLayout.LayoutParams.MATCH_PARENT;
    params.width = RelativeLayout.LayoutParams.MATCH_PARENT;
    sr.setLayoutParams(params);

    PropertyValuesHolder pvhLeft = PropertyValuesHolder.ofInt("left", 0, 1);
    PropertyValuesHolder pvhTop = PropertyValuesHolder.ofInt("top", 0, 1);
    PropertyValuesHolder pvhRight = PropertyValuesHolder.ofInt("right", 0, 1);
    PropertyValuesHolder pvhBottom = PropertyValuesHolder.ofInt("bottom", 0, 1);
    PropertyValuesHolder pvhRoundness = PropertyValuesHolder.ofFloat("roundness", 0, 1);


    final Animator collapseExpandAnim = ObjectAnimator.ofPropertyValuesHolder(sr, pvhLeft, pvhTop,
            pvhRight, pvhBottom, pvhRoundness);
    collapseExpandAnim.setupStartValues();

    sr.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
        @Override
        public boolean onPreDraw() {
            sr.getViewTreeObserver().removeOnPreDrawListener(this);
            collapseExpandAnim.setupEndValues();
            collapseExpandAnim.start();
            return false;
        }
    });
}

但是,通常 android:animateLayoutChanges="true" 应该可以。

有关 Chet Haase 的详细信息,请参阅:https://www.youtube.com/watch?v=55wLsaWpQ4g

因为 RelativeLayout 扩展了 View,所以您可以使用在 "regular views" 上使用的所有动画。例如尝试使用 animate.scale() 看看它是否适合你。一种简单的方法是

@Override
protected void onCreate (Bundle savedInstanceState) {

   sr = (RelativeLayout) findViewById(view.getId());
   sv.setVisibility(View.GONE);

   scaleEnd = sr.getScaleX();
   sr.setScaleX(0);
}

...
@Override
protected void onPostCreate (Bundle savedInstanceState) {
    super.onPostCreate(savedInstanceState);
    sr.setVisibility(View.Visible);
    sr.animate.scaleX(scaleEnd);
}

此外,如果您想要 Lollipop 类型的动画,请查看:Use the Reveal Effect

对我有用的简单技巧:

添加:

android:animateLayoutChanges="true"

到您正在更改其布局参数的视图的 xml。

然后,在您的 Java/Kotlin 代码中添加这行代码,然后再更改参数:

Java:

((ViewGroup) sr).getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);

科特林:

(sr as ViewGroup).layoutTransition.enableTransitionType(LayoutTransition.CHANGING)