Pinker
Pinker

Reputation: 65

How to get right values from Views touch event

I have a problem with implementing touch events on GLSurfaceView. Views size is 1280x696, because of android (tablet) status bar at bottom with soft keys, time etc.., (screen resolution is 1280x800), but OnTouchListener is receiving touch events with coords [646.0,739.0], and thus my gluunproject method fails to return correct values is there any way to return events that respect these boundaries? or how should I recalculate the position?

Upvotes: 0

Views: 188

Answers (1)

Matic Oblak
Matic Oblak

Reputation: 16774

In general to transition between such coordinate systems: If you transition from system A to B and you have points

  • AOrigin (left upper corner of your receiver view)
  • AEnd (right bottom corner of your receiver view (origin + size))
  • BOrigin (left upper corner of your GL view)
  • BEnd (right bottom corner of your GL view (origin + size))

where AOrigin and BOrigin represent the same location in view and same goes for AEnd and BEnd then for point P in the receiver view:

X = B.Origin.x + ((P.x - AOrigin.x)/(AEnd.x - AOrigin.x)) * (BEnd.x - B.Origin.x)
Y = B.Origin.y + ((P.y - AOrigin.y)/(AEnd.y - AOrigin.y)) * (BEnd.y - B.Origin.y)

For your case (I'm not sure because of lack of information) AOrigin is at (0,0), AEnd is at (646, 739), BOrigin is at (0,0) and BEnd is at (1280, 800):

X = P.x/646 * 1280
Y = P.y/739 * 800

You can also use this to transition to/from "GL" coordinates. A common case is having a view with upper left corner at (0, 0) and lower bottom at (1280, 800) and your "GL" coordinates are from (-1, 1) to (1, -1):

X = -1 + (P.x/1280)*2
Y = 1 + (P.y/800)*(-2)

Also note that you may use any 2 pairs of points that represent same location on screen as long as (origin-end).x != 0 AND (origin-end).y != 0

Upvotes: 2

Related Questions