动画后设置布局参数

时间:2012-07-18 11:52:00

标签: android animation layout

我在RelativeLayout中有2个观点,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <View
        android:id="@+id/view1"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:background="@drawable/shape_red"/>

    <View
        android:id="@+id/view2"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_below="@id/view1"
        android:background="@drawable/shape_blue"/>

</RelativeLayout>

现在,我将view1的y属性设置为0到300.但我希望view2根据view1更改其位置 - 因为view2设置为在下面(布局 - 下面)view1

由于这不起作用,我试图在动画中添加一个onAnimationEnd侦听器。像这样:

    ObjectAnimator ani = ObjectAnimator.ofFloat(view1, "Y", 200);
    ani.setDuration(2000);
    ani.addListener(new AnimatorListenerAdapter()
    {
        @Override
        public void onAnimationEnd(Animator animation)
        {
            LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, view2.getHeight());
            params.addRule(RelativeLayout.BELOW, view1.getId());

            view2.setLayoutParams(params);
        }
    });

view2根本没有改变其立场。

如果可能的话,我想避免为view2制作新动画,所以请不要费心去推荐。 ;)

有没有人有一些建议?

1 个答案:

答案 0 :(得分:-1)

检查是否可以解决您的问题:

ani.addListener(new AnimatorListenerAdapter()
    {
        @Override
        public void onAnimationEnd(Animator animation)
        {
            LayoutParams params = view2.getLayoutParams();
            // change it ...
            params.addRule(RelativeLayout.BELOW, view1.getId());

            view2.setLayoutParams(params);
        }
    });