私はそれを行う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());
}