Adding multiple touch listeners inside canvas Andr

2019-08-31 01:17发布

Suppose I have multiple drawings in my canvas, how can I add touch listener for each of the drawings seperately inside that canvas?

I will be having different drawings like star, circle, rectangle etc etc added dynamically how can I handle each of them ?

I couldn find ans in SO the very close question was this Set touch listeners on canvas drawings but it has no ans.

Help and suggestions will be appreciated. Thanks in advance :)

1条回答
Lonely孤独者°
2楼-- · 2019-08-31 01:44

Actually you can't add any touch listener on the drawings of your canvas. You have to add the listener in your view then onTouchEvent you will get the x, y coordinates of user touch then have to calculate by your own if the touch location is any of your drawings .

For example

@Override
public boolean onTouchEvent(MotionEvent event){
        int touchX, touchY;

    touchX = (int) event.getX();
    touchY = (int) event.getY();

    if ((touchX > (cntWidth / 2) && touchX < (scrnWidth - (cntWidth / 2)))) && ((touchY > cntHeight / 2 && touchY < (scrnHeight - (cntHeight / 2)))) {                      
        int col, row;

        // Here you will check

    }
}
查看更多
登录 后发表回答