我可以想到一种方法,但我确信必须有一种更清洁的方法。
将 mainView 所在的布局设置为 FrameLayout(或 RelativeLayout),并将 ImageView 包含在其中,使其成为 mainView 的兄弟,但将其列在 mainView 之前。layout_gravity="bottom"使用(或者layout_alignParentBottom="true"如果使用RelativeLayout)将其设置为底部。
现在将 ObjectAnimator 中的目标更改为 mainView 上方的内容(因此可以是其容器视图或 Activity/Fragment),并将属性更改为“scrollUp”之类的内容,然后创建一个setScrollUp(float)在目标中命名的方法。在此方法中,使用setTranslationY(). 按其高度偏移阴影。
在一个使用纯色的简单应用程序中为我工作:
XML:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              android:background="#f00">
    <View
        android:id="@+id/shadow"
        android:layout_width="match_parent"
        android:layout_height="20px"
        android:layout_gravity="bottom"
        android:background="#00f"/>
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#0f0"
        android:id="@+id/container"/>
</FrameLayout>
活动代码:
@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    container = findViewById(R.id.container);
    shadow = findViewById(R.id.shadow);
    container.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(final View v)
        {
            final float translationTo = (open) ? 0 : -300;
            final float translationFrom = (open) ? -300 : 0;
            open = !open;
            ObjectAnimator anim = ObjectAnimator.ofFloat(MyActivity.this, "scrollUp", translationFrom, translationTo);
            anim.setDuration(500);
            anim.start();
        }
    });
}
public void setScrollUp(final float position)
{
    container.setTranslationY(position);
    shadow.setTranslationY(position + shadow.getHeight());
}