protected void onMeasure (int widthMeasureSpec, int heightMeasureSpec)
onMeasure传入的两个参数是由上一层控件传入的大小,有多种情况,重写该方法时需要对计算控件的实际大小,然后调用setMeasuredDimension(int, int)设置实际大小。
int getMode(int measureSpec)//根据提供的测量值(格式)提取模式(上述三个模式之一)
int getSize(int measureSpec)//根据提供的测量值(格式)提取大小值(这个大小也就是我们通常所说的大小)
int makeMeasureSpec(int size,int mode)//根据提供的大小值和模式创建一个测量值(格式)
int mode = MeasureSpec.getMode(widthMeasureSpec)
int size = MeasureSpec.getSize(widthMeasureSpec)
MeasureSpec.UNSPECIFIED, MeasureSpec.EXACTLY, MeasureSpec.AT_MOST
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int width = MeasureSpec.getSize(widthMeasureSpec);
int height = MeasureSpec.getSize(heightMeasureSpec);
if (0 == mRatioWidth || 0 == mRatioHeight) {
setMeasuredDimension(width, height);
} else {
if (width < height * mRatioWidth / mRatioHeight) {
setMeasuredDimension(width, width * mRatioHeight / mRatioWidth);
} else {
setMeasuredDimension(height * mRatioWidth / mRatioHeight, height);
}
}
}
public void fitWindow(int width, int height) {
if (width < 0 || height < 0) {
throw new IllegalArgumentException("Size cannot be negative.");
}
mRatioWidth = width;//屏幕宽
mRatioHeight = height;//屏幕高
requestLayout();//促使调用onMeasure
}