Adding multiple touch listeners inside canvas Android

让人想犯罪 __ 提交于 2019-12-25 08:15:46

问题


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:


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

    }
}


来源:https://stackoverflow.com/questions/22930048/adding-multiple-touch-listeners-inside-canvas-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!