0

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 :)

Community
  • 1
  • 1
ik024
  • 3,566
  • 7
  • 38
  • 61
  • see my answer here http://stackoverflow.com/questions/21633545/android-imageview-scaling-and-translating-issue – pskink Apr 08 '14 at 07:32
  • 1
    short version: you have to store your shapes and see if the X and Y of onTouch matches(with offset) any of the shapes X and Y, this is how I done it. – cesztoszule Apr 08 '14 at 07:35

1 Answers1

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

    }
}
Chinmoy Debnath
  • 2,814
  • 16
  • 20