Frage

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?

War es hilfreich?

Lösung 3

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.

Andere Tipps

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.

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.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top