-->

Android Screen Size Algorithm / Pattern

2020-04-15 05:37发布

问题:

I'm looking for an Algorithm or Pattern to calculate where objects can be placed on multiple resolutions. So far, I have got the X and Y screen size, but im not sure how to turn it into a calculation that would place something such as a drawText() at a location on the screen no matter the screen size.

I was thinking perhaps using percentages might be easier to work with. I have a program that draws text to the screen at the top left corner indicating what position the screen has been touched.

when i run this on the emulator, with the

drawText(info, 10,10, paint);

it displays great, but when i run it on my phone (Droid 2 Global) the top of the text is cut off.

In short:

Is there any way to draw something to the screen (using SurfaceView) that will remain in the same spot over multiple screen dimensions / dpi?

Thanks for your time.

回答1:

It's no perfect solution that i've seen so far.

I came across this issue by specifying the position for the particular item for the specific screen ratio(native screen resolution in an emulator), then recalculate its position and scale it up/down when running it in the different screen size.

  displayXpos =  constDevelopmentScreenXpos (targetDeviceScreenHeight/constDevelopmentScreenHeight) etc..
  displayXScale = similarAlgorithm

This is not the best, but it give you some idea.

Also, i fill some 'limbo' area with a background and choose to not care it when the target device X:Y screen ratio is different from development device.

HTH



回答2:

ya, it is better to use calculation in percentage

first get the total size avail of the screen, then calculate percentage on it and the place the control based on calculation

to get avail size

Display mDisplay= activity.getWindowManager().getDefaultDisplay();
int width= mDisplay.getWidth();
int height= mDisplay.getHeight();