第一步: 复写
onDraw方法,在方法中画出我们的视图(字符串为例)
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
paint = new Paint();
// 填充消除据此
paint.setStyle(Paint.Style.FILL);
paint.setFlags(Paint.ANTI_ALIAS_FLAG);
String string = "画字符串";
// 对于的xy坐标是文本左下角的坐标
canvas.drawText(string, 10, 100, paint);
//得到所画字符串的高宽
Rect rect = new Rect();
paint.getTextBounds(string, 0, string.length(), rect);
int with=rect.width();
int height=rect.height();
// 利用xy坐标和高宽得到字符在view中的矩形框
RectF rectF=new RectF();
rectF.left=10;
rectF.bottom=100;
rectF.right=rectF.left+with;
rectF.top=rectF.bottom-height;
// 得到监听view
this.rectF=rectF;
}
第二步 :生成一个自定义的监听,来监听我们自定义事件