最终效果
activity_main 布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent">
<Button
android:id="@+id/btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="点击"/>
<LinearLayout
android:id="@+id/ll_view"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0"
android:orientation="vertical"
android:background="#ff00"/>
</LinearLayout>
Java代码
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
//设置高度
height = 500;
show = true;
btn = findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(show){
//显示view,高度从0变到height值
va = ValueAnimator.ofInt(0,height);
show = false;
}else{
//隐藏view,高度从height变为0
va = ValueAnimator.ofInt(height,0);
show = true;
}
va.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator valueAnimator) {
//获取当前的height值
int h =(Integer)valueAnimator.getAnimatedValue();
//动态更新view的高度
ll_view.getLayoutParams().height = h;
ll_view.requestLayout();
Log.d("ss", h+"");
}
});
va.setDuration(1000);
//开始动画
va.start();
}
});
ll_view = findViewById(R.id.ll_view);
}
}
网友评论