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

私はそれを行う1つの方法を考えることができますが、よりクリーンな方法があるに違いないと確信しています.

mainView が存在するレイアウトを FrameLayout (または RelativeLayout) にし、その中に ImageView を含めて mainView の兄弟にしますが、mainView の前にリストします。layout_gravity="bottom"を使用して(またはlayout_alignParentBottom="true"RelativeLayout を使用している場合は)一番下になるように設定します。

ここで、ObjectAnimator のターゲットを mainView の上にあるもの (コンテナ ビューまたはアクティビティ/フラグメントのいずれか) に変更し、プロパティを「scrollUp」などに変更してからsetScrollUp(float)、ターゲットで名前が付けられたメソッドを作成します。このメソッドでは、 を使用して mainView とシャドウの変換を設定し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 に答える