Android 动画 GONE 可见性

Android Animate GONE visibility

在我的应用程序中,我有 RelativeLayout 和任何小部件,进入布局,我想通过 xml 动画将其向上移动。 RelativeLayout 可见性为 GONE,必须再次将可见性设置为 GONE 并将其向上移动。

我的问题是,再次将可见性设置为 GONE 后,移动动画不起作用,将可见性更改为 VISIBLE 后,RelativeLayout 可以显示,但只有孩子们向上移动,我想 RelativeLayout 将所有辣椒移动到顶部。如何解决这个问题?

XML动画:

<?xml version="1.0" encoding="utf-8"?>
<set
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:interpolator="@android:anim/linear_interpolator"
        android:fillAfter="true">

    <translate
            android:fromYDelta="70%p"
            android:toYDelta="0%p"
            android:duration="800" />
</set>

动画代码:

animMoveUp = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.animation_move_up);
uiFiller.toolbarLinearlayoutIcons.setVisibility(View.VISIBLE);
uiFiller.toolbarLinearlayoutIcons.startAnimation(animMoveUp);

我的布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/main"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#ffe67f24"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/main_tsms_actionbar_background" >

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="38dp"
            android:layout_height="38dp" />

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:gravity="right"
            android:orientation="vertical" >

            <TextView
                android:id="@+id/txtSmsReceiveSlaveMobile"
                android:layout_width="fill_parent"
                android:layout_height="0dp"
                android:layout_weight="1" />

            <TextView
                android:id="@+id/txtSmsReceiveSlaveContactName"
                android:layout_width="fill_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:gravity="right|center_vertical"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:textColor="#f6f6f6"
                android:textSize="@dimen/normal_text_size" />
        </LinearLayout>
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="#ffffff"
        android:minHeight="150dp"
        android:orientation="horizontal" >

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:orientation="vertical" >

            <ListView
                android:id="@+id/lvSmsReceiveSlaveList"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@android:color/transparent"
                android:divider="@null"
                android:listSelector="@android:color/transparent"
                android:stackFromBottom="true"
                tools:listitem="@layout/activity_sms_receive_slave_item" >
            </ListView>
        </LinearLayout>
    </LinearLayout>

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="1dp"
        android:background="@drawable/gradient_divider" />

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="50dp" >

        <TextView
            android:id="@+id/sms_counter"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="false"
            android:layout_alignParentTop="false"
            android:gravity="left"
            android:paddingLeft="3dp"
            android:paddingTop="3dp"
            android:textColor="#ff787878"
            android:textSize="9sp" />

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:background="#ffffff" >

            <ImageButton
                android:id="@+id/imgbtn_send_sms"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="5dp"
                android:layout_marginTop="8dp"
                android:background="@drawable/icon_send_sms" />

            <EditText
                android:id="@+id/smsBody"
                android:layout_width="0dp"
                android:layout_height="fill_parent"
                android:layout_gravity="right|center_vertical"
                android:layout_weight="1"
                android:background="#ffffff"
                android:ems="10"
                android:gravity="right|top"
                android:hint="@string/hint_enter_text"
                android:paddingBottom="5dp"
                android:paddingLeft="10dp"
                android:paddingRight="10dp"
                android:paddingTop="5dp"
                android:textColor="#000000"
                android:textSize="@dimen/small_text_size"
                android:windowSoftInputMode="adjustResize" >

                <requestFocus />
            </EditText>
        </LinearLayout>
    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/toolbarLinearlayoutIcons"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="bottom"
        android:animateLayoutChanges="true"
        android:visibility="gone" >

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="48dp"
            android:orientation="horizontal" >

            <ImageButton
                android:id="@+id/imgbtn_copy_to_clipboard"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginLeft="10dp"
                android:background="@drawable/icon_copy_to_clipboard" />

            <TextView
                android:layout_width="0dp"
                android:layout_height="fill_parent"
                android:layout_weight="1" />

            <ImageButton
                android:id="@+id/imgbtn_delete_sms"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:background="@drawable/icon_delete_sms" />

            <TextView
                android:id="@+id/TextView02"
                android:layout_width="0dp"
                android:layout_height="fill_parent"
                android:layout_weight="1" />

            <ImageButton
                android:id="@+id/imgbtn_share_sms"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:background="@drawable/icon_share_sms" />

            <TextView
                android:id="@+id/TextView03"
                android:layout_width="0dp"
                android:layout_height="fill_parent"
                android:layout_weight="1" />

            <ImageButton
                android:id="@+id/imgbtn_forward_sms"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginRight="10dp"
                android:background="@drawable/icon_forward_sms" />
        </LinearLayout>

    </RelativeLayout>

</LinearLayout>

在此布局中我想移动 toolbarLinearlayoutIcons id

使用以下方法展开和折叠所需的视图:

public void expand(final View v) {
    v.measure(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
    final int targtetHeight = v.getMeasuredHeight();
    if (v.isShown()) {
        collapse(v);
    } else {
        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) (targtetHeight * interpolatedTime);
                v.requestLayout();
            }

            @Override
            public boolean willChangeBounds() {
                return true;
            }
        };
        a.setDuration((int) (targtetHeight + 500));
        v.startAnimation(a);
    }

}

public void collapse(final View v) {
    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;
        }
    };

    a.setDuration((int) (v.getLayoutParams().height + 500));
    v.startAnimation(a);
}

您可以在 xml 中使用 1 行,只需将其放在父布局中即可

    android:animateLayoutChanges="true"