- Notifications
You must be signed in to change notification settings - Fork 91
CustomView
sloop edited this page Jun 6, 2016
·
1 revision
自定义View基类,如果你的自定义View继承这个类则能自动获取到View大小与一个默认到Paint,可以帮助你节省部分代码,示例如下:
publicclassMyViewextendsView {
privateintmWidth;
privateintmHeight;
privatePaintmPaint;
publicMyView(Contextcontext) {
this(context, null);
}
publicMyView(Contextcontext, AttributeSetattrs) {
super(context, attrs);
// 初始化画笔mPaint.setColor(Color.GRAY);
}
@OverrideprotectedvoidonSizeChanged(intw, inth, intoldw, intoldh) {
super.onSizeChanged(w, h, oldw, oldh);
mWidth = w;
mHeight = h;
}
@OverrideprotectedvoidonDraw(Canvascanvas) {
super.onDraw(canvas);
// 获取View宽高与画笔并根据此绘制内容canvas.translate(mWidth / 2, mHeight / 2);
canvas.drawCircle(0,0,100,mPaint);
}
}publicclassMyViewextendsCustomView {
publicMyView(Contextcontext) {
this(context, null);
}
publicMyView(Contextcontext, AttributeSetattrs) {
super(context, attrs);
// 初始化画笔mDefaultTextPaint.setColor(Color.GRAY);
}
@OverrideprotectedvoidonDraw(Canvascanvas) {
super.onDraw(canvas);
// 获取View宽高与画笔并根据此绘制内容canvas.translate(mViewWidth / 2, mViewHeight / 2);
canvas.drawCircle(0,0,100,mDeafultPaint);
}
}