我通过扩展View创建自定义视图,在画布上绘制一些圆圈,现在我想知道用户点击的圆圈或圆圈索引,我怎么能处理这个,我没有找到任何Circle类,所以我可以有一个Circles列表 下面是我的代码----
@Override
protected void onDraw(Canvas canvas) {
int width = canvas.getWidth();
int height = canvas.getHeight();
System.out.println("width : " + width + ", Height : " + height);
paint.setColor(Color.BLACK);
paint.setStrokeWidth(3);
float ballDiameter = width / ((COLUMN_COUNT > ROW_COUNT) ? COLUMN_COUNT : ROW_COUNT);
float ballRadius = ballDiameter / 2;
Random random = new Random();
for (int i = 0; i < ROW_COUNT; i++) {
float cy = ballRadius + i * ballDiameter;
for (int j = 0; j < COLUMN_COUNT; j++) {
int nextInt = random.nextInt();
if (nextInt % 4 == 0) {
paint.setStyle(Paint.Style.FILL);
} else {
paint.setStyle(Paint.Style.STROKE);
}
float cx = ballRadius + j * ballDiameter;
canvas.drawCircle(cx, cy, ballRadius, paint);
}
}
}
答案 0 :(得分:0)
快速示例
float touchx;
float touchy;
float ballDiameter = width / ((COLUMN_COUNT > ROW_COUNT) ? COLUMN_COUNT : ROW_COUNT);
float ballRadius = ballDiameter / 2;
for (int i = 0; i < ROW_COUNT; i++) {
float cy = ballRadius + i * ballDiameter;
for (int j = 0; j < COLUMN_COUNT; j++) {
float cx = ballRadius + j * ballDiameter;
if(Math.sqrt(Math.pow(touchx - cx, 2) + Math.pow(touchy- cy, 2))<ballRadius){
//CIRCLE cx,cy was touched
}
}
}