<pre name="code" class="java"><span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);"><span style="font-size:18px;">Activity代碼如下</span></span>
<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);"><span style="font-size:18px;">
</span></span>
<span style="font-size:18px;">import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.view.animation.AccelerateInterpolator;
import android.widget.Button;
import android.widget.LinearLayout;
public class MainActivity extends AppCompatActivity {
private LinearLayout contentPanel;
private Button expand_btn;
private int height;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
contentPanel= (LinearLayout) findViewById(R.id.contentPanel);
expand_btn= (Button) findViewById(R.id.expand_btn);
contentPanel.measure(0, 0);
height=contentPanel.getMeasuredHeight();
}
public void to(View v){
if(contentPanel.isShown()){
contraction();
}else{
expand();
}
}
/**
* 收縮
*/
private void contraction(){
AnimatorSet set=new AnimatorSet();
//根據值的改變,動態設置view的高度
ValueAnimator valueAnimator=ValueAnimator.ofInt(height,0);
valueAnimator.setDuration(1000);
valueAnimator.setInterpolator(new AccelerateInterpolator());
valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
int height = (int) animation.getAnimatedValue();
Log.e("height", height + "");
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, height);
contentPanel.setLayoutParams(params);
}
});
valueAnimator.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
contentPanel.setVisibility(View.GONE);
expand_btn.setText("展開");
}
});
//將view從不透明變成透明
ObjectAnimator alpha=ObjectAnimator.ofFloat(contentPanel,"alpha",1f,0f);
alpha.setDuration(1000);
set.play(valueAnimator).with(alpha);
set.start();
}
/**
* 展開
*/
private void expand(){
contentPanel.setVisibility(View.VISIBLE);
AnimatorSet set=new AnimatorSet();
ValueAnimator valueAnimator=ValueAnimator.ofInt(0,height);
valueAnimator.setDuration(1000);
valueAnimator.setInterpolator(new AccelerateInterpolator());
valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
int height = (int) animation.getAnimatedValue();
Log.e("height", height + "");
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, height);
contentPanel.setLayoutParams(params);
}
});
valueAnimator.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
expand_btn.setText("收起");
}
});
//將view從透明變成不透明
ObjectAnimator alpha=ObjectAnimator.ofFloat(contentPanel,"alpha",0f,1f);
alpha.setDuration(1000);
set.play(valueAnimator).with(alpha);
set.start();
}
}
</span></span>
XML代碼:
<span style="font-family: Arial, Helvetica, sans-serif;"><span style="font-size:18px;"><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"</span></span>
<span style="font-size:18px;"> xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<Button
android:id="@+id/expand_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="to"
android:text="展開"/>
<LinearLayout
android:id="@+id/contentPanel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="11111111111111111111111111111111111111"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="11111111111111111111111111111111111111"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="11111111111111111111111111111111111111"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="11111111111111111111111111111111111111"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="11111111111111111111111111111111111111"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="11111111111111111111111111111111111111"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="11111111111111111111111111111111111111"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="11111111111111111111111111111111111111"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="11111111111111111111111111111111111111"/>
</LinearLayout>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=""/>
</LinearLayout>
</span></span>