image.png
自定義view
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int mode = MeasureSpec.getMode(widthMeasureSpec);
int size = MeasureSpec.getSize(widthMeasureSpec);
int heightSize = MeasureSpec.getSize(heightMeasureSpec);
if(mode == MeasureSpec.AT_MOST){
setMeasuredDimension(500,heightSize);
}
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
int width = getWidth();
int height = getHeight();
int paddingLeft = getPaddingLeft();
int paddingRight = getPaddingRight();
int paddingBottom = getPaddingBottom();
int paddingTop = getPaddingTop();
canvas.drawCircle((width+paddingLeft-paddingRight)/2,(height-paddingBottom+paddingTop)/2,Math.min((width-paddingLeft-paddingRight)/2,(height-paddingBottom-paddingTop)/2),paint);
}
自定義viewgroup,對onMeasure和onLayout的處理
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
//假設(shè)這是一個linearlayout vertical 具體情況具體討論
//
int widthMode = MeasureSpec.getMode(widthMeasureSpec);
int heightMode = MeasureSpec.getMode(heightMeasureSpec);
int widthSize = MeasureSpec.getSize(widthMeasureSpec);
int heightSize = MeasureSpec.getSize(heightMeasureSpec);
measureChildren(widthMeasureSpec,heightMeasureSpec);
//調(diào)用了measureChild 里面合成了view的MeasureSpec髓涯,然后view調(diào)用了measure
//如果mode 是EXACTLY 按照自己的size去設(shè)置 這里不寫了
//如果是AL_MOST 遍歷子元素袒啼,寬度是最大子元素寬度,高度是所有子元素加起來的高度。
//1.判斷子元素?cái)?shù)量 //2.遍歷子元素 獲取寬高 //3.給viewgroup設(shè)置寬高
int childCount = getChildCount();
//如果是0 直接寬高設(shè)為0
//如果不是
int viewMeasurHeight = 0;
int viewMeasuredWidth = 0;
for (int i =0;i<childCount;i++){
View childAt = getChildAt(i);
viewMeasurHeight = childAt.getMeasuredHeight();
viewMeasuredWidth = childAt.getMeasuredWidth();
//假設(shè)每個view的寬高固定
break;
}
setMeasuredDimension(viewMeasuredWidth,viewMeasurHeight*childCount);
//實(shí)際情況中還要考慮view的margin,和自己的padding
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
//還是以onMeasure做假設(shè)的 線性布局蚓再,Vertical
//每一個view 從上到下排列滑肉。x坐標(biāo)不變,y坐標(biāo)一直變大摘仅。
int childCount = getChildCount();
for (int i = 0;i<childCount;i++){
View view = getChildAt(i);
int measuredHeight = view.getMeasuredHeight();
int measuredWidth = view.getMeasuredWidth();
//四個坐標(biāo)
view.layout(0,measuredHeight*i,measuredWidth,measuredHeight*(i+1));
//如果把viewgroup的padding 和view的margin考慮進(jìn)去
int paddingLeft = getPaddingLeft();
int paddingRight = getPaddingRight();
//top 和bottom不寫了 一樣的情況靶庙。
MarginLayoutParams layoutParams = (MarginLayoutParams) view.getLayoutParams();
int leftMargin = layoutParams.leftMargin;
int rightMargin = layoutParams.rightMargin;
view.layout(leftMargin+paddingLeft,measuredHeight*i,measuredWidth+paddingRight+rightMargin,measuredHeight*(i+1));
}
}
public MyVIewCircle(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.MyVIewCircle);
int color = typedArray.getColor(R.styleable.MyVIewCircle_color, Color.RED);
typedArray.recycle();
init();
}