a standard onMeasure method
在Android自定义View中,一个标准的onMeasure()方法。
1 | @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); int viewWidth = circleRadius /*默认size*/+ this.getPaddingLeft() + this.getPaddingRight(); int viewHeight = circleRadius + this.getPaddingTop() + this.getPaddingBottom(); int widthMode = MeasureSpec.getMode(widthMeasureSpec);//获取宽度的模式 int widthSize = MeasureSpec.getSize(widthMeasureSpec);//获取宽度的尺寸 int heightMode = MeasureSpec.getMode(heightMeasureSpec);//高度的模式 int heightSize = MeasureSpec.getSize(heightMeasureSpec);//高端的尺寸 int width; int height; //Measure Width if (widthMode == MeasureSpec.EXACTLY) {//指定了确定的尺寸:100dp,match_parent //Must be this size width = widthSize; } else if (widthMode == MeasureSpec.AT_MOST) {//wrap_content类型 //Can't be bigger than... width = Math.min(viewWidth, widthSize); } else { //Be whatever you want width = viewWidth; } //Measure Height if (heightMode == MeasureSpec.EXACTLY || widthMode == MeasureSpec.EXACTLY) { //Must be this size height = heightSize; } else if (heightMode == MeasureSpec.AT_MOST) { //Can't be bigger than... height = Math.min(viewHeight, heightSize); } else { //Be whatever you want height = viewHeight; } setMeasuredDimension(width, height); } |