自定义ViewGroup,对设置layoutParam的bottomMargin不太理解
package com.aiyuba.animateview;
import android.animation.Animator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.RotateAnimation;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;
import com.aiyuba.uiview.R;
/**
* Created by maoyujiao on 2020/4/11.
*/
public class ExpandView extends FrameLayout {
private static final String TAG = "ExpandView";
private final View view;
private TextView expand;
private ImageView arrow;
private boolean isClose = true;
private RotateAnimation rotateAnimation;
public ExpandView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
view = LayoutInflater.from(context).inflate(R.layout.expand_view,this,true);
expand = view.findViewById(R.id.expand);
arrow = view.findViewById(R.id.img_arrow);
arrow.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
initAnimation();
}
});
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
//默认隐藏
final MarginLayoutParams marginLayoutParams = (MarginLayoutParams) expand.getLayoutParams();
int expandHeight = expand.getMeasuredHeight();
marginLayoutParams.bottomMargin = -expandHeight;
expand.setLayoutParams(marginLayoutParams);
}
private void initAnimation() {
if(isClose) {
rotateAnimation = new RotateAnimation(0, 180,
RotateAnimation.RELATIVE_TO_SELF, 0.5f,
RotateAnimation.RELATIVE_TO_SELF, 0.5f);
}else {
rotateAnimation = new RotateAnimation(180, 360,
RotateAnimation.RELATIVE_TO_SELF, 0.5f,
RotateAnimation.RELATIVE_TO_SELF, 0.5f);
}
rotateAnimation.setDuration(500);
rotateAnimation.setFillAfter(true);
arrow.startAnimation(rotateAnimation);
final MarginLayoutParams marginLayoutParams = (MarginLayoutParams) expand.getLayoutParams();
int expandHeight = expand.getMeasuredHeight();
ValueAnimator valueAnimator = isClose ? ValueAnimator.ofInt(-expandHeight,0)
:ValueAnimator.ofInt(0,-expandHeight);
valueAnimator.setDuration(500);
valueAnimator.start();
valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
int height = (int)animation.getAnimatedValue();
Log.d(TAG, "onAnimationUpdate: " + height);
marginLayoutParams.bottomMargin = height;//不太懂bottomMargin的作用
// marginLayoutParams.height = height; //设置height第一次可以,当height变为0后,getMeasuredHeight就一直为0了
expand.setLayoutParams(marginLayoutParams);
}
});
valueAnimator.addListener(new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animation) {
}
@Override
public void onAnimationEnd(Animator animation) {
isClose = !isClose;
}
@Override
public void onAnimationCancel(Animator animation) {
}
@Override
public void onAnimationRepeat(Animator animation) {
}
});
}
}
效果: