<Edit> 支持要求(见 cmets):
据我所知,以下是您所追求的。支持您的要求所需的一项更改是使用 FrameLayout 作为父容器。
现在的布局是:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fl"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white" >
<RelativeLayout
android:id="@+id/rl1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/holo_blue_bright" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."
android:textColor="@android:color/black"
android:layout_marginTop="100dp" />
</RelativeLayout>
<RelativeLayout
android:id="@+id/rl2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/holo_blue_dark" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."
android:textColor="@android:color/black"
android:layout_alignParentBottom="true"
android:layout_marginBottom="200dp" />
</RelativeLayout>
<Button
android:id="@+id/bClickToAnimate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click to Resize" />
</FrameLayout>
Java 代码:
FrameLayout fl;
RelativeLayout rl1;
RelativeLayout rl2;
Button b;
boolean isOverlapping;
int toTranslate;
@Override
protected void onCreate(Bundle savedInstanceState) {
....
rl1.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
rl2.getViewTreeObserver().removeOnGlobalLayoutListener(this);
// Get width of the viewgroup
int width = rl1.getWidth();
// We will keep the second viewgroup outside bounds - we need
// FrameLayout for this
FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) rl2.getLayoutParams();
lp.leftMargin = width;
lp.width = width;
rl2.setLayoutParams(lp);
// Amount to translate
toTranslate = width;
}
});
b.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// Viewgroup `rl2` is currently not overlapping
// Translate `rl2` over `rl1`
if (!isOverlapping) {
ObjectAnimator oa = ObjectAnimator.ofFloat(rl2,
"translationX", 0, -toTranslate);
oa.setDuration(2000L);
oa.setInterpolator(new AccelerateInterpolator(2.0f));
oa.start();
} else {
// Viewgroup `rl2` is currently overlapping
// Translate `rl2` off `rl1`
ObjectAnimator oa = ObjectAnimator.ofFloat(rl2,
"translationX", -toTranslate, 0);
oa.setDuration(2000L);
oa.setInterpolator(new AccelerateInterpolator(2.0f));
oa.start();
}
}
});
}
</Edit>
其实这很简单。来看看吧:
您只需要以下内容:
// Animate property `right` - final width is zero
ObjectAnimator oa = ObjectAnimator.ofInt(YOUR_VIEWGROUP, "right",
YOUR_VIEWGROUP.getWidth(), 0);
// Set animation duration - 5 seconds
oa.setDuration(5000L);
// Set interpolator
oa.setInterpolator(new AccelerateInterpolator(2.0f));
// Start animation
oa.start();
如果您的应用的minSdkVersion 低于 11,请使用 NineOldAndroids 库支持 > api 8。
我正在使用一个相当简单的布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@android:color/white" >
<Button
android:id="@+id/bClickToAnimate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click to Resize" />
<RelativeLayout
android:id="@+id/rlToAnimate"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/holo_blue_bright" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Lorem ipsum dolor sit amet, consectetur
adipisicing elit, sed do eiusmod tempor
incididunt ut labore et dolore magna aliqua."
android:textColor="@android:color/black"
android:layout_centerVertical="true" />
</RelativeLayout>
</LinearLayout>
动画的视图组是rlToAnimate。