Android: How to find SurfaceView touch position when fixed Size is set? -


i have surfaceview android game development has holder.setfixedsize(1280, 720);. added few sprites on it. want action when sprite touched. following surfaceview.ontouch code getting x , y value of screen instead of surface view xy.

please me find out actual xy of surface view.

@override public boolean ontouch(view view, motionevent event) {     int x = (int) event.getx();     int y = (int) event.gety();      switch (event.getaction()) {         case motionevent.action_down: {             if (scenes.size() > 0) {                 scene scene = scenes.get(scenes.size() - 1);                 if (scene != null) {                     int size = scene.getsprites().size();                     log.i(tag, "surface touched. size : " + size);                     (int j = size - 1; j >= 0; j--) {                         sprite s = scene.getsprites().get(j);                         if (s.istouchable() && s.performtouch(x, y)) {                             log.i(tag, "touch handled sprite");                             return true;                         }                     }                 }             }              break;         }         case motionevent.action_move: {              break;         }     }      return false; } 

change computation of x , y shown below:

int x = (int) (event.getx() * 1280 / view.getwidth()); int y = (int) (event.gety() * 720 / view.getheight()); 

Comments

Popular posts from this blog

Sort a complex associative array in PHP -

vb.net - How to ignore if a cell is empty nothing -

recursion - Can every recursive algorithm be improved with dynamic programming? -