线性布局可见时为可见性模式设置动画

时间:2016-04-06 07:48:55

标签: android animation

我有如下线性布局

<LinearLayout
    android:id="@+id/pilihwaktu"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="125dp">

    <com.testing.CustomLinearLayout
        android:id="@+id/oneway"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:background="@drawable/field_background"
        android:padding="5dp"
        android:layout_weight=".50">

        ....
   </com.testing.CustomLinearLayout>

    <com.testing.CustomLinearLayout
        android:id="@+id/roundtrip"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:background="@drawable/field_background"
        android:padding="5dp"
        android:layout_weight=".50">

        ....

    </com.testing.CustomLinearLayout>

</LinearLayout>

它会显示这样的视图

+---------------+---------------+
|               |               |
|    oneway     |   roundtrip   |
|               |               |
+---------------+---------------+

我想让往返作为默认设置消失,当选中Checkbox时,我想要设置往返动画,以便将事物恢复到上述状态(并且单向部分将动画,因为它看起来很平滑)。并且当未选中复选框时,往返将动画隐藏或消失

我试图按照link进行操作,但它没有动画,看起来像ObjectAnimator很简单!

这样做的正确方法是什么?

1 个答案:

答案 0 :(得分:2)

简单方法:

首先,将此属性添加到您的父LinearLayout:

<LinearLayout
   android:id="@+id/pilihwaktu"
   ...
   android:animateLayoutChanges="true"
   ... />

然后在代码中设置检查更改侦听器,如下所示:

yourCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
       @Override
       public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
           roundTip.setVisibility(isChecked ? View.VISIBLE : View.GONE);
       }
});  

更难的方式:

这是为了扩展,它很容易让它崩溃。如果已选中,则在onCheckedChange中使用此

updateListener = new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                // Get params:
                final LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) roundTip.getLayoutParams();

                params.weight = (Float) animation.getAnimatedValue();
                roundTip.setLayoutParams(loparams);
                }
        };

animatorListener = new AnimatorListenerAdapter() {
    @Override
    public void onAnimationStart(Animator animation) {
        roundTip.setVisibility(View.VISIBLE);     
    }
};

animator = ValueAnimator.ofFloat(0f, 0.5f);
animator.addUpdateListener(updateListener); 
animator.addListener(animatorListener);            
animator.setDuration(500);
animator.start();
相关问题