1

I'm implementing an OnTouchListener on a custom view. In this view I'm drawing an object that basically moves along x and y axis I get form event.getX(); and event.getY(); respectively.

I want to perform some draw changes when touch events happen at particular positions in screen. The problem is, the values are different for different screen sizes. For example value of X value of a point on a WVGA device is 60 and on tablets it is 100. Since this is inconsistent i couldn't write a proper if condition.

Please can any one suggest a solution for this from your prior experience?

Wesley
  • 1,808
  • 5
  • 31
  • 46

3 Answers3

1

You could set the basic screen size that you want to receive touch events on ie 800 x 600. Then you can get the device's actual resolution, and scale your base amount by that. Ie the X size would be (800/ device width) you then times your device screen touch coords by this scale factor to get the "translated" version.

Davos555
  • 1,974
  • 15
  • 23
  • Thanks for the quick reply. Please don't mind my innocence. I'm not able to get the algorithm correctly. Please help. My View is in fill_parent so screenWidth = getMeasuredWidth() => of view. I'm confused with the scale factor that you are mentioning about. Thanks in advance. – Wesley Jul 05 '12 at 15:43
  • Say you developed your app on a screen size of 300x200. You want to detect when the user touchs the left side of the screen. So you'd have if(touch.x < 150){ //do something} Then if you install it on something with a screen size of 600x200, then if(x < 150) would only work if you touch the far left of the screen, not from the halfway point. So what you need to do, is scale the touch.x value. So you'd have X on your device (300), X on new device (600). 300 / 600 = 0.5... So any touch event coords you receive you multiply them by 0.5 to get them relative to your if statement params. – Davos555 Jul 05 '12 at 16:26
1

You could get any device's resolution. You can do something like, getting getWidth() and getHeigth() of screen. Then you can set logic for width/2 and heigth/2 -> gives you Left-Top portion of your screen. Do something like this.

Chintan Raghwani
  • 3,370
  • 4
  • 22
  • 33
0

I basically got good ideas with both answers, (my respect) But the solution that worked for me is below. The idea is from the clue by Chintan Raghwani.

So lets say a position x value on a device screen is equal to the total view width multiplied by percentage

Example: posX = getMeasuredWidth() * 0.5 will give you touch event handled at center. Positioning objects by this way will make it consistent through all devices.

Hope it helps some one :)

EDIT :

Lucky enough to find this post. This is process above is also called as the Golden Ratio this is really a great post about this.

Wesley
  • 1,808
  • 5
  • 31
  • 46