简体   繁体   中英

Using values from getX() and getY() for different resolution devices (relative scale)

I've done some scouring for the past hour on this topic, and I have found some promising results, but none of them have done what I needed them to do. Pretty much, I am using XY coordinates to trigger a sound, and this works great. Lets say I want to divide a screen into 8 squares, and have each square trigger a different sound. I can do this pretty easily, except there is one major problem. If I am using the X and Y values for my emulator (Which is 800x440 pixels), and then using the same program on my EVO 3D (which i believe is 960x540), the whole process is essentially ruined, because I have a portion of my screen being used, rather than the whole screen. Is there a way to use something similar to density pixels instead of true X and Y values for touchEvents? That way everything scales for each particular device. I've tried using getRawX()/Y and that doesn't seem to change anything at all; it's still pixel for pixel, rather than a relative scale, that would work for each device. Can anyone direct me to a method around this? Thanks a lot, I appreciate it!

EDIT: I figured it out, thanks to some simple advice from mice. Heres what I did for those who are wondering:

    x = e.getX();
    y = e.getY();       
    DisplayMetrics gettotalY = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(gettotalY);       
    toty = gettotaly.heightPixels;
    ym1 = (float) (toty * 0.5);     

I can't see problem here. MotionEvent.getX/Y return pixel values relative to lef/top corner of application's window, up to its size. getRawX/Y return also pixel values, but relative to entire screen.

It's then up to you how you use this pixel values. Eg divide it by window's width/height and you get float values in range 0-1, on any screen resolution. Or do other math.

That's just simple computations, and DPI doesn't play role here. But it depends on where you draw (if you do) graphics where user is supposed to touch screen.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM