在特定位置显示图像OnClick Android

时间:2014-01-08 11:20:05

标签: 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);
        }
    }

使用

.OnClickListener

怎么可能?

2 个答案:

答案 0 :(得分:0)

覆盖onTouchEvent

使用实例变量float x,y

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

使用xy绘制图像。

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.

您需要知道圆的中心和半径以检测圆的触摸。

我的这会帮助你理解 Creating a spray effect on touch draw in android

答案 1 :(得分:0)

如果覆盖onTouchEven,则可以获得触摸的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:
}

return true }

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