2016-04-06 6 views
0

のLinearLayout私はアニメーションの可視性モード見える

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

ようなビューが表示されますときチェックボックスをオンにすると、私はラウンドトリップをアニメートして、上の状態に戻るようにします(そして、onewayセクションは、次のように滑らかに見えるようにアニメーションされます)。 ...チェックボックスは往復非表示にアニメーション化されます確認または

を行っていないときに、私はこの linkに従うことを試みたが、それは、アニメーションとObjectAnimatorはそれを行うための簡単な方法があるように見えていなかった

これを行う正しい方法は何ですか?

答えて

2

簡単な方法:まず

、この属性のLinearLayoutあなたの親に追加します。

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

:このようなコードで

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

以降のセット確認変更リスナーをもっと難しい方法:

これは拡張用ですが、折りたたむのは簡単です。それがチェックされます場合

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(); 
+0

おかげで、onCheckedChangeでこれを使用して...私は...聞いて素敵な – meeftah

+0

@meeftahを簡単な方法を選択してください:) –