在android中动画视图高度

时间:2016-03-05 15:31:09

标签: android android-animation android-xml

我有一个带有按钮的视图持有者,可以在同一版面中切换其他视图的可见性(我在ID locals_additional_filters 中切换视图的可见性)。 现在,通过使视图可见,父布局高度( id为host_filter_holder )正在更改,我的问题是如何设置父布局高度的动画?

这是我的xml:

<LinearLayout
        android:id="@+id/host_filter_holder"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/red_borders_btn"
        android:orientation="vertical">

        <Button
            android:id="@+id/find_host_filter"
            android:layout_width="match_parent"
            android:layout_height="45dp"
            android:text="@string/more_filters"
            android:background="@null"
            android:textColor="@color/red_borders"
            android:textSize="16sp" />

        <LinearLayout
            android:id="@+id/locals_additional_filters"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:orientation="vertical"
            android:padding="10dp"
            android:animateLayoutChanges="true"
            android:visibility="gone">

            <RadioGroup
                android:id="@+id/locals_gender_group"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal">

                <RadioButton
                    android:id="@+id/female"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:checked="true"
                    android:text="Female"
                    android:textColor="@color/black" />

                <RadioButton
                    android:id="@+id/male"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:text="Male"
                    android:textColor="@color/black" />
            </RadioGroup>

            <EditText
                style="@style/NoBottomLineEditText"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="10dp"
                android:clickable="true"
                android:drawableEnd="@drawable/ic_arrow"
                android:drawableRight="@drawable/ic_arrow"
                android:hint="Search your friends in 2km" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="@string/interests"
                android:textColor="@color/dark_gray"
                android:textSize="14sp" />

            <android.support.v7.widget.RecyclerView
                android:id="@+id/locals_filter_recycle"
                android:layout_width="match_parent"
                android:layout_height="40dp" />
        </LinearLayout>

    </LinearLayout>

2 个答案:

答案 0 :(得分:0)

使用ValueAnimator或ObjectAnimator:

LinearLayout container = findViewById(R.id.host_filter_holder);
Button btn = findViewById(find_host_filter);
LinearLayout changeBox = findViewById(R.id.locals_additional_filters);
 ValueAnimator animator = ValueAnimator.ofInt(btn.getHeight() + 
                changeBox.getHeight(),btn.getHeight()); 


animator.setTarget(container );  
animator.setDuration(1000).start();  
animator.addUpdateListener(new AnimatorUpdateListener()  
{  
    @Override  
    public void onAnimationUpdate(ValueAnimator animation)  
    {   
        LayoutParams layoutparams = container.getLayoutParams();
        layoutparams.height = animation.getAnimatedValue();
        container.setLayoutParams(layoutparams );
    }  
});  

答案 1 :(得分:0)

使用Value animator非常简单:

myView.animate().setScaleY(anyValue).SetDuration(durationInMills);