效果图:
代码:
动画工具类
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从不可见变为可见的动画,原理:动态改变其LayoutParams.height的值
* @param view 要展示动画的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从可见变为不可见的动画,原理:动态改变其LayoutParams.height的值
* @param view 要展示动画的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();
}
}
/**
* 动态改变view的高度动画效果,动画时长300毫秒[android属性动画默认时长]
* 原理:动画改变view LayoutParams.height的值
* @param view 要进行高度改变动画的view
* @param startHeight 动画前的view的高度
* @param endHeight 动画后的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();
}
}
}
Activity代码
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;
}
}
}
布局文件
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center">