// max = background.getWidth() - button.getWidth();// left = isOpen ? max : 0;
scaledTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop(); // 滑动临界值
加载完成
加载完成时调用onFinishInflate()
测量
设置控件大小
调用setMeasuredDimension设置控件大小,以背景大小为准
@OverrideprotectedvoidonMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int width = measureDimension(widthMeasureSpec, background.getWidth());
int height = measureDimension(heightMeasureSpec, background.getHeight());
setMeasuredDimension(width, height); // 测量完成后设置控件大小
}
measureSpec 为32位二进制数,前2位表示Mode,后30位表示Size
一般测量方法
privateintmeasureDimension(int measureSpec, int content) {
int result = 0;
int mode = MeasureSpec.getMode(measureSpec);
int size = MeasureSpec.getSize(measureSpec);
switch (mode) {
case MeasureSpec.UNSPECIFIED: // 未指定,例如ScrollView
result = content;
break;
case MeasureSpec.EXACTLY: // 确定值,match_parent和确定的数值
result = size;
break;
case MeasureSpec.AT_MOST: // 最大值,wrap_content
result = Math.min(content, size); // 在最大可用值和内容的大小中取最小值break;
}
return result;
}
缩放
测量完成,调用onSizeChanged(int w, int h, int oldw, int oldh)
调整控件各部分大小
@OverrideprotectedvoidonSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
button = Bitmap.createScaledBitmap(button, button.getWidth() * w / background.getWidth(), button.getHeight() * h / background.getHeight(), true);
background = Bitmap.createScaledBitmap(background, w, h, true);
// 此时需初始化和大小相关的变量
max = background.getWidth() - button.getWidth();
left = isOpen ? max : 0;
}
布局
调用onLayout(boolean changed, int left, int top, int right, int bottom),对子View进行布局