Android: RelativeLayout translateY 动画 layout_alignParentBottom

Android: RelativeLayout translateY animation with layout_alignParentBottom

我正在开发一个带有动画的 Android 应用程序,其中垂直居中的视图应该从屏幕的中心到顶部进行动画处理。此视图下方是另一个包含相关内容的视图,它直接对齐在中心视图下方。当中心视图向上动画时,我希望下方视图 'pinned' 到屏幕顶部,但也到屏幕底部(根据 layout_alignParentBottom="true")。不过目前,随着底部视图向上动画,底部视图和屏幕底部之间会留下一个间隙:

为底部视图设置动画,同时将其固定在屏幕底部的最简单方法是什么?

这是我的布局XML:

<?xml version="1.0" encoding="utf-8"?>
<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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="newsoni.com.testrelativelayouttranslation.MainActivity">

    <View
        android:layout_width="match_parent"
        android:layout_height="20dp"
        android:background="#FF0000"
        android:layout_centerVertical="true"
        android:id="@+id/centerBar" />
    <View
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:background="#00FF00"
        android:layout_below="@+id/centerBar"
        android:id="@+id/bottomPanel" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Do animation"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:id="@+id/btn" />

</RelativeLayout>

这是我的 Java:

public class MainActivity extends AppCompatActivity {

    private View mCenter, mBottomPanel;
    private Button mBtn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mCenter = findViewById(R.id.centerBar);
        mBottomPanel = findViewById(R.id.bottomPanel);
        mBtn = $(R.id.btn);

        mBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                int value = mCenter.getTranslationY() == 0 ? -mCenter.getTop() : 0;
                mCenter.animate()
                        .translationY(value)
                        .setDuration(250)
                        .start();
                mBottomPanel.animate()
                        .translationY(value)
                        .setDuration(250)
                        .start();
            }
        });

    }

    @SuppressWarnings("unchecked")
    public <T extends View> T $(int id) {
        return (T) findViewById(id);
    }
}

这里是一个link项目,您可以下载:

https://drive.google.com/file/d/0B-mqMIMqm_XHamxENkhIZDJDMHM/view?usp=sharing

我最终使用标准 Animation 并覆盖 applyTransformation 以手动更改底部视图的 LayoutParams