效果圖:
代碼:
動(dòng)畫工具類
<pre>
package test.j.com.test.utils;
import android.animation.ValueAnimator;
import android.view.View;
import android.view.ViewGroup;
/**
- Created by Jiangzx on 11:53.
*/
public class AnimationUtils {
/**
* 將view從不可見變?yōu)榭梢姷膭?dòng)畫胆描,原理:動(dòng)態(tài)改變其LayoutParams.height的值
* @param view 要展示動(dòng)畫的view
*/
public static void visibleAnimator(final View view){
if(view!=null) {
int viewHeight=view.getHeight();
if(viewHeight==0){
int width=View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED);
int height=View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED);
view.measure(width,height);
viewHeight=view.getMeasuredHeight();
}
ValueAnimator animator=ValueAnimator.ofInt(0,viewHeight);
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
ViewGroup.LayoutParams params=view.getLayoutParams();
params.height= (int) animation.getAnimatedValue();
view.setLayoutParams(params);
}
});
animator.start();
}
}
/**
* 將view從可見變?yōu)椴豢梢姷膭?dòng)畫,原理:動(dòng)態(tài)改變其LayoutParams.height的值
* @param view 要展示動(dòng)畫的view
*/
public static void invisibleAnimator(final View view){
if(view!=null){
int viewHeight=view.getHeight();
if(viewHeight==0){
int width=View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED);
int height=View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED);
view.measure(width,height);
viewHeight=view.getMeasuredHeight();
}
ValueAnimator animator=ValueAnimator.ofInt(viewHeight,0);
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
ViewGroup.LayoutParams params=view.getLayoutParams();
params.height= (int) animation.getAnimatedValue();
view.setLayoutParams(params);
}
});
animator.start();
}
}
/**
* 動(dòng)態(tài)改變view的高度動(dòng)畫效果巨双,動(dòng)畫時(shí)長300毫秒[android屬性動(dòng)畫默認(rèn)時(shí)長]
* 原理:動(dòng)畫改變view LayoutParams.height的值
* @param view 要進(jìn)行高度改變動(dòng)畫的view
* @param startHeight 動(dòng)畫前的view的高度
* @param endHeight 動(dòng)畫后的view的高度
*/
public static void changeViewHeightAnimatorStart(final View view,final int startHeight,final int endHeight){
if(view!=null&&startHeight>=0&&endHeight>=0){
ValueAnimator animator=ValueAnimator.ofInt(startHeight,endHeight);
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
ViewGroup.LayoutParams params=view.getLayoutParams();
params.height= (int) animation.getAnimatedValue();
view.setLayoutParams(params);
}
});
animator.start();
}
}
}
</pre>
Activity代碼
<pre>
package test.j.com.test.ui;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.TypedValue;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import test.j.com.test.R;
import test.j.com.test.utils.AnimationUtils;
/**
- Created by Jiangzx on 11:52.
*/
public class AnimatorActivity extends AppCompatActivity implements View.OnClickListener{
Button btControlLayout;
Button btControlView;
LinearLayout llContainer;
TextView tvHeight0;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_animator);
btControlLayout = (Button) findViewById(R.id.bt_control_layout);
btControlLayout.setTag(0);
btControlView = (Button) findViewById(R.id.bt_control_view);
btControlView.setTag(0);
llContainer= (LinearLayout) findViewById(R.id.ll_container);
tvHeight0= (TextView) findViewById(R.id.tv_height_0);
btControlLayout.setOnClickListener(this);
btControlView.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.bt_control_layout:
if(((int)(btControlLayout.getTag()))==0){
btControlLayout.setTag(1);
btControlLayout.setText("收起布局");
AnimationUtils.visibleAnimator(llContainer);
}else{
btControlLayout.setTag(0);
btControlLayout.setText("展開布局");
AnimationUtils.invisibleAnimator(llContainer);
}
break;
case R.id.bt_control_view:
if(((int)(btControlView.getTag()))==0){
btControlView.setTag(1);
btControlView.setText("隱藏控件");
AnimationUtils.changeViewHeightAnimatorStart(tvHeight0,
0,
(int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,150,
getResources().getDisplayMetrics()));
}else{
btControlView.setTag(0);
btControlView.setText("顯示控件");
AnimationUtils.changeViewHeightAnimatorStart(tvHeight0,
(int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,150,
getResources().getDisplayMetrics()),
0);
}
break;
}
}
}
</pre>
布局文件
<pre>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center">
<Button
android:id="@+id/bt_control_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="展開布局"/>
<Button
android:id="@+id/bt_control_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="顯示控件"/>
</LinearLayout>
<LinearLayout
android:id="@+id/ll_container"
android:layout_width="match_parent"
android:layout_height="0dp"
android:divider="@color/colorPrimary"
android:showDividers="middle"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="固定高度文本,高度50dp蝉衣,居中顯示"
android:textColor="@color/colorPrimaryDark"
android:gravity="center"/>
<TextView
android:layout_width="match_parent"
android:layout_height="150dp"
android:text="固定高度文本匆骗,高度150dp,居中顯示"
android:textColor="@color/colorPrimaryDark"
android:gravity="center"/>
</LinearLayout>
<TextView
android:id="@+id/tv_height_0"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginTop="100dp"
android:text="初始高度為0的文本初始高度為0的文本初始高度為0的文本初始高度為0的文本初始高度為0的文本初始高度為0的文本初始高度為0的文本初始高度為0的文本初始高度為0的文本"
android:textColor="@color/colorAccent"/>
</LinearLayout>
</pre>