2013-12-16 86 views
0

我通过扩展视图创建自定义视图,在画布上绘制一些圆圈,现在我想知道用户点击的圆圈或圆圈索引,我该如何处理,我没有找到任何Circle类,所以我可以有一个圈子列表。
下面是我的代码----如何获取在画布上点击的圆圈

@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); 
     } 
    } 
} 
+1

有在画布中没有这样的事情。你*绘制*一个圆,而不是*创建一个圆对象。您需要处理视图上的触摸,然后确定触摸是否位于绘制的圆圈内 - 这意味着您需要跟踪绘制的圆圈。 – 323go

+0

我得到x,y位置用户点击,但如何获得基于x,y位置的圆,有没有任何库方法得到它? – umesh

+0

我看到你没看过我的评论。请这样做。 – 323go

回答

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 
     } 

    } 
}