0

我有一个阴影的 9 个补丁图像,我想将它添加到RelativeLayout. 布局填满屏幕,但在用户点击按钮后向上滑动。所以,我想让阴影图像低于RelativeLayout(以负底部边距下拉),这样当布局朝上时,阴影位于布局的底部边缘,从而产生分层效果。

<ImageView
            android:id="@+id/shadow"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            android:layout_marginBottom="-10dp"
            android:src="@drawable/shadow" />

我正在使用以下方法向上滑动框架:

ObjectAnimator mover = ObjectAnimator.ofFloat(mainView, "translationY", !historyShown ? -ph : 0);
 mover.setDuration(300);
 mover.start();

奇怪的是,当我将图像添加到布局并给它一个负边距时,它只是没有显示,就像它切断了布局边界之外的任何东西。

有没有解决的办法?

4

1 回答 1

2

我可以想到一种方法,但我确信必须有一种更清洁的方法。

将 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());
}
于 2013-10-07T16:25:20.793 回答