- 测量方法 onMeasure()
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
返回的参数是32位,高两位是测量模式。后30位是尺寸
private static final int MODE_SHIFT = 30;
/**
* 这个模式,我也不知道他干嘛的,只要在xml里设置了,高两位都不会是00。至于没有设置的情况。。。。这个还真的不知道哦
* Measure specification mode: The parent has not imposed any constraint
* on the child. It can be whatever size it wants.
*/
public static final int UNSPECIFIED = 0 << MODE_SHIFT;
/**
* 精准模式,如果当match_parent和一个固定的尺寸时,返回。
* 这个模式下。子控件的大小是充满父布局,或者在自己想要的尺 * 寸下。子控件的大小收到父控件的影响
* Measure specification mode: The parent has determined an exact size
* for the child. The child is going to be given those bounds regardless
* of how big it wants to be.
*/
public static final int EXACTLY = 1 << MODE_SHIFT;
/**
*如果是wrap_content
*这个模式下,控件的大小是尽可能的去得到自己的大小。根据控件内容自填充。
* Measure specification mode: The child can be as large as it wants up
* to the specified size.
*/
public static final int AT_MOST = 2 << MODE_SHIFT;
这个方法是用来测量控件尺寸的。
// 返回的mode是32位int。后30位是0。
int mode = MeasureSpec.getMode(measureSpec);
int size = MeasureSpec.getSize(measureSpec);
获取到测量模式和尺寸,
- 绘制View onDraw()
protected void onDraw(Canvas canvas)
通过这个方法来绘制view,通过画布Canvas和画笔Paint共同绘制。
一些Paint的设置
Paint
- setAnitAlias(boolean aa)
去画笔锯齿
- setStyle(Style style)
画笔的样式
public enum Style {
/**
* 将充满
* Geometry and text drawn with this style will be filled, ignoring all
* stroke-related settings in the paint.
*/
FILL (0),
/** 绘制空心的状态
* Geometry and text drawn with this style will be stroked, respecting
* the stroke-related fields on the paint.
*/
STROKE (1),
/**
* Geometry and text drawn with this style will be both filled and
* stroked at the same time, respecting the stroke-related fields on
* the paint. This mode can give unexpected results if the geometry
* is oriented counter-clockwise. This restriction does not apply to
* either FILL or STROKE.
*/
FILL_AND_STROKE (2);
Style(int nativeInt) {
this.nativeInt = nativeInt;
}
final int nativeInt;
}
-
setDither(boolean dither)
图片设置抖动处理
-
setColor(int color);
设置画笔的颜色
-
setStrokeWidth(float width)
设置线条的宽度
mPaint = new Paint();
// 设置颜色
mPaint.setColor(Color.BLUE);
// 设置填充状态
mPaint.setStyle(Paint.Style.STROKE);
// 设置线条宽度
mPaint.setStrokeWidth(3);
// 去锯齿
mPaint.setAntiAlias(true);

网友评论