【问题标题】:Animate visibility modes when linearlayout visible线性布局可见时为可见性模式设置动画
【发布时间】:2016-04-06 07:48:55
【问题描述】:

我有如下线性布局

<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   |
|               |               |
+---------------+---------------+

我想使往返作为默认值以及检查复选框时,我想动画往返,因此它将将事物恢复到上述状态(并且单向部分将动画如下,所以它看起来很平滑)。并且当未选中复选框时,往返将动画隐藏或消失

我尝试关注这个link,但它没有动画,看起来 ObjectAnimator 是一种简单的方法......

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

【问题讨论】:

    标签: android animation


    【解决方案1】:

    简单的方法:

    首先,将这个属性添加到你的父 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();
    

    【讨论】:

    • @meeftah 很高兴听到 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-28
    • 2012-03-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多