簡體   English   中英

動畫后設置布局參數

[英]setting layout params after animation

我在這樣的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制作新的動畫,所以請不要建議一個。 ;)

有人有建議嗎?

檢查是否可以解決您的問題:

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);
        }
    });

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM