Java 单击Android在特定位置显示图像

Java 单击Android在特定位置显示图像,java,android,onclicklistener,Java,Android,Onclicklistener,我有一行7个圆圈和6列,我想根据使用Onclick单击的圆圈显示图像 int rows, cols; rows = 7; cols = 6; for (int i=0; i <rows; i++) { for (int j=0; j< cols; j++) { canvas.drawCircle(90 + (100*i), 155 + (115*j), 40, white); } } 这怎么可能呢?覆

我有一行7个圆圈和6列,我想根据使用Onclick单击的圆圈显示图像

int rows, cols;
    rows = 7;
    cols = 6;
    for (int i=0; i <rows; i++) {
        for (int j=0; j< cols; j++) {
        canvas.drawCircle(90 + (100*i),  155 + (115*j), 40, white);
        }
    }

这怎么可能呢?

覆盖
ontochevent

使用实例变量
float x,y

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        x = event.getX(); 
        y = event.getY();
        invalidate(); // to refresh draw
        return true;
    }
使用
x
y
绘制图像

public boolean onTouchEvent (MotionEvent event)

Added in API level 1
Implement this method to handle touch screen motion events.

If this method is used to detect click actions, it is recommended that the actions be performed by implementing and calling performClick(). This will ensure consistent system behavior, including:

obeying click sound preferences
dispatching OnClickListener calls
handling ACTION_CLICK when accessibility features are enabled
Parameters
event   The motion event.
Returns
True if the event was handled, false otherwise.
您需要知道圆的中心和半径,才能检测与圆的接触

我想这有助于你理解

如果覆盖Ontouch,即使可以获得触摸的x和y

@Override 
public boolean onTouchEvent(MotionEvent event) {
    int x = (int)event.getX();
    int y = (int)event.getY();
    switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
        case MotionEvent.ACTION_MOVE:
        case MotionEvent.ACTION_UP:
}
返回true
}


现在,如果使用onTouchEvent中的x和y值使用drawCircle函数,您将准确地在您触摸的位置绘制它。

覆盖onTouch获取坐标x和y并调用invalidate刷新绘图您是否愿意将解决方案更改为GridView?它使您可以更轻松地执行所需操作,包括设置原始表格如何获取x和y坐标?因为它在for loopno栅格视图中,我正在使用一个自定义viewFor loop来显示圆圈,我无法删除它们,我想单击这些圆圈,根据所选的圆圈,图像应该显示在其中,它是一个connect 4game@user3146973我不明白。您应该更具体。@user3146973您如何知道单击了哪个圆?你知道每个圆的半径吗?我如何添加图像?@user3146973 canvas有一个drawImage方法这些圆应该保持原样,这是一个connect 4游戏。根据单击的圆圈,我希望计数器位于该圆圈中
@Override 
public boolean onTouchEvent(MotionEvent event) {
    int x = (int)event.getX();
    int y = (int)event.getY();
    switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
        case MotionEvent.ACTION_MOVE:
        case MotionEvent.ACTION_UP:
}