0

我使用下面的代碼來展開特定的LinearLayout,並按照本教程 http://gmariotti.blogspot.sg/2013/09/expand-and-collapse-animation.html的Android的LinearLayout擴大

但動畫是不是真的光滑,有什麼理由?

佈局XML

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" 
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    tools:context="com.techiequickie.bharath.parsetest.NewBet"> 

    <LinearLayout 
     android:id = "@+id/mainLinear" 
     android:orientation="vertical" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_alignParentTop="true" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" 
     android:padding="30px" 
     android:weightSum="1"> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="@string/bet_name_text" 
      android:id="@+id/tv_Betname" 
      android:textSize="@dimen/text_size_general"/> 

     <EditText 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:id="@+id/et_Betname" /> 

     <Spinner 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:id="@+id/spin_Betcat1" 
      android:spinnerMode="dropdown" 
      android:entries="@array/bet_categories"/> 

     <Spinner 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:id="@+id/spin_Betcat2" 
      android:spinnerMode="dropdown" 
      android:paddingTop="@dimen/spin_categories_space_between" 
      android:entries="@array/bet_categories2"/> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="@string/bet_value_text" 
      android:id="@+id/bv_Betvalue" 
      android:textSize="@dimen/text_size_general"/> 

     <SeekBar 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:id="@+id/sb_Betvalue" 
      android:max="10" 
      android:progress="0" 
      android:indeterminate="false" /> 

     <LinearLayout 
      android:orientation="horizontal" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="0.15"> 

      <Switch 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="Real Bet" 
       android:id="@+id/switch_Betreal" 
       android:checked="false" /> 
     </LinearLayout> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="@string/bet_description" 
      android:id="@+id/textView6" 
      android:textSize="@dimen/text_size_general" /> 
    <LinearLayout 
     android:id="@+id/collapsable" 
     android:orientation="horizontal" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="0.71"> 

     <EditText 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:inputType="textMultiLine" 
      android:ems="10" 
      android:id="@+id/editText2" 
      android:layout_weight="1" /> 
    </LinearLayout> 

     <Button 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:id="@+id/btn_Placebet" 
      android:layout_gravity="center_horizontal" 
      android:text="@string/place_bet_button" /> 

    </LinearLayout> 
</RelativeLayout> 

活動課

package com.techiequickie.bharath.parsetest; 

import android.animation.*; 
import android.support.v7.app.ActionBarActivity; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.LinearLayout; 


public class NewBet extends ActionBarActivity { 

    LinearLayout mainlayout, collapsablelayout; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_new_bet); 

     mainlayout = (LinearLayout) findViewById(R.id.mainLinear); 
     collapsablelayout = (LinearLayout) findViewById(R.id.collapsable); 

     collapsablelayout.setVisibility(View.GONE); 

     mainlayout.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       if(collapsablelayout.getVisibility()==View.GONE) 
       { 
        expand(); 
       } 
       else 
       { 
        collapse(); 
       } 
      } 
     }); 
    } 

    private void expand() 
    { 
     collapsablelayout.setVisibility(View.VISIBLE); 

     final int widthSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED); 
     final int heightSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED); 
     collapsablelayout.measure(widthSpec, heightSpec); 

     ValueAnimator mAnimator = slideAnimator(0, collapsablelayout.getMeasuredHeight()); 
     mAnimator.start(); 
    } 

    private void collapse() { 
     int finalHeight = collapsablelayout.getHeight(); 

     ValueAnimator mAnimator = slideAnimator(finalHeight, 0); 

     mAnimator.addListener(new Animator.AnimatorListener() { 
      @Override 
      public void onAnimationStart(Animator animation) { 

      } 

      @Override 
      public void onAnimationEnd(Animator animator) { 
       //Height=0, but it set visibility to GONE 
       collapsablelayout.setVisibility(View.GONE); 
      } 

      @Override 
      public void onAnimationCancel(Animator animation) { 

      } 

      @Override 
      public void onAnimationRepeat(Animator animation) { 

      } 

     }); 
     mAnimator.start(); 
    } 

    private ValueAnimator slideAnimator(int start, int end) 
    { 

     ValueAnimator animator = ValueAnimator.ofInt(start, end); 

     animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { 
      @Override 
      public void onAnimationUpdate(ValueAnimator valueAnimator) { 
       //Update Height 
       int value = (Integer) valueAnimator.getAnimatedValue(); 
       ViewGroup.LayoutParams layoutParams = collapsablelayout.getLayoutParams(); 
       layoutParams.height = value; 
       collapsablelayout.setLayoutParams(layoutParams); 
      } 
     }); 
     return animator; 
    } 


    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.menu_new_bet, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     // Handle action bar item clicks here. The action bar will 
     // automatically handle clicks on the Home/Up button, so long 
     // as you specify a parent activity in AndroidManifest.xml. 
     int id = item.getItemId(); 

     //noinspection SimplifiableIfStatement 
     if (id == R.id.action_settings) { 
      return true; 
     } 

     return super.onOptionsItemSelected(item); 
    } 
} 

回答

0

嘗試加入android:animateLayoutChanges="true"的線性佈局

+0

是增加這主要佈局也使動畫更流暢。 – JackyBoi 2015-04-01 08:22:45