通过动画增加线性布局的高度



我想在2秒钟后增加线性布局的高度。加载活动时,应用程序应等待2秒,然后线性布局的高度应从200dp增加到300dp。xml代码如下所示。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#fffafaf9"
android:clipChildren="false"
 tools:context=".MainActivity">
<TextView android:text="@string/hello_world"
    android:id="@+id/top"
    android:layout_width="wrap_content"
    android:layout_height="60dp"
    android:paddingTop="30dp"
    android:paddingLeft="20dp"
    />
<View
    android:layout_width="fill_parent"
    android:layout_height="2dp"
    android:layout_below="@id/top"
    android:background="#ffd3d3d2" />
<LinearLayout android:layout_width="fill_parent"
    android:layout_height="200dp"
    android:layout_alignParentBottom="true"
    android:orientation="vertical">
    <View
    android:id="@+id/temp_view"
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:visibility="visible"
    android:translationZ="20dp"
    android:layout_gravity="center_horizontal"
    android:background="@drawable/circle_shape"
    />
<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="200dp"
    android:layout_marginTop="-80px"
    android:background="#ffe4e4e3"
    android:elevation="-10dp"
    android:alpha="0.5"
    android:layout_below="@id/temp_view"
    >

</LinearLayout>

您可以编写这样的自定义LinearLayout:

public class CustomLinearLayout extends LinearLayout {
private int mLayoutHeight;
public CustomLinearLayout(Context context, AttributeSet attrs) {
    super(context, attrs);
}
public void setLayoutHeight(int height) {
    mLayoutHeight = height;
    ViewGroup.LayoutParams lp = getLayoutParams();
    lp.height = height;
    setLayoutParams(lp);
}
public int getLayoutHeight() {
    return mLayoutHeight;
}

}

然后在你的活动中找到LinearLayout并开始动画:

 customLayout = (CustomLinearLayout) findViewById(R.id.cl);
    ObjectAnimator oa = ObjectAnimator.ofInt(customLayout, "layoutHeight", DensityUtil.dip2px(this, 200), DensityUtil.dip2px(this, 300));
    oa.setDuration(2000);
    oa.start();

最新更新