0

我正在尝试使用登录表单创建一个简单的活动。我想要做的是将应用程序的徽标放在登录按钮的正上方,当用户按下登录按钮时,徽标会向上滑动一点,并且在徽标和登录按钮之间有一个登录表单(用户名,密码字段)淡入。由于我是 Android 动画的新手,我需要一些帮助。

我有以下布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="match_parent">

    <ImageView android:id="@+id/logo"
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:layout_marginBottom="8dp"
               android:src="@drawable/logo"
               android:layout_centerHorizontal="true"
               android:layout_above="@+id/login_form"/>

    <LinearLayout android:id="@id/login_form"
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:orientation="vertical"
                  android:layout_above="@+id/sign_in"
                  android:visibility="gone">

        <EditText android:id="@+id/username"
                  android:layout_width="match_parent"
                  android:layout_height="48dp"
                  android:layout_marginBottom="8dp"
                  android:textColor="@color/Black"
                  android:textColorHint="@color/Black"
                  android:textIsSelectable="false"
                  android:hint="@string/username"/>

        <EditText android:id="@+id/password"
                  android:layout_width="match_parent"
                  android:layout_height="48dp"
                  android:inputType="textPassword"
                  android:textColor="@color/Black"
                  android:textColorHint="@color/Black"
                  android:textIsSelectable="false"
                  android:hint="@string/password"/>

    </LinearLayout>

    <Button android:id="@id/sign_in"
            android:layout_width="match_parent"
            android:layout_height="48dp"
            android:layout_marginTop="8dp"
            android:layout_alignParentBottom="true"
            android:textColor="@color/White"
            android:textSize="18sp">
</RelativeLayout>

当使用下面的任何动画时,标志要么突然向上跳跃几个像素然后表格淡入,要么向上滑动几个像素然后突然向上跳跃然后表格淡入。

mForm.setAlpha(0.0f);
mForm.setScaleX(0.0f);
mForm.setScaleY(0.0f);
mForm.setVisibility(View.VISIBLE);

mForm.animate()
        .alpha(1.0f)
        .scaleX(1.0f)
        .scaleY(1.0f)
        .setInterpolator(new AccelerateDecelerateInterpolator())
        .setDuration(mAnimationDurationMedium);

<>

mLogo.animate()
    .translationYBy(mDisplayMetrisUtils.dpToPixels(-96.0f))
    .setDuration(mAnimationDurationMedium)
    .setInterpolator(new AccelerateDecelerateInterpolator())
    .withEndAction(new Runnable() {
        @Override
        public void run() {
            mForm.setAlpha(0.0f);
            mForm.setScaleX(0.0f);
            mForm.setScaleY(0.0f);
            mForm.setVisibility(View.VISIBLE);

            mForm.animate()
                    .alpha(1.0f)
                    .scaleX(1.0f)
                    .scaleY(1.0f)
                    .setInterpolator(new AccelerateDecelerateInterpolator())
                    .setDuration(mAnimationDurationMedium);
        }
    });

<>

mForm.setAlpha(0.0f);
mForm.setScaleX(0.0f);
mForm.setScaleY(0.0f);
mForm.setVisibility(View.VISIBLE);

mForm.animate()
        .alpha(1.0f)
        .scaleX(1.0f)
        .scaleY(1.0f)
        .setInterpolator(new AccelerateDecelerateInterpolator())
        .setDuration(mAnimationDurationMedium)
        .withStartAction(new Runnable() {
            @Override
            public void run() {
                mLogo.animate()
                        .translationYBy(-mForm.getHeight())
                        .setInterpolator(new AccelerateDecelerateInterpolator())
                        .setDuration(mAnimationDurationMedium);
            }
        });

据我了解,即使我将 LinearLayout 的 x 和 y 比例设置为 0,一旦它变为可见,即使它的比例设置为 0,它也会占用一次显示它所需的所有必要像素,因此突然向上跳跃徽标。不过,我想要实现的是让 LinearLayout 逐渐占据更多空间,并为徽标提供平滑的向上滑动效果。

我试过向黑暗领主祈祷,但没有成功。有人可以告诉我我错过了什么并指出正确的方向吗?有没有办法创建一个动画,将 LinearLayout 的高度从 0dp 转换为 wrap_content?

在此先感谢,迪米特里斯。

4

1 回答 1

1

您是否考虑过使用带有 animateLayoutChanges 标志的 LinearLayout?

我认为这可以给你几乎零工作的预期效果。用 LinearLayout 替换您的外部 RelativeLayout,然后当您更改表单的可见性时,它将为其入口设置动画。

有关文档,请参阅此链接:http: //developer.android.com/training/animation/layout.html

于 2015-03-25T10:50:12.060 回答