Android Screen Size Algorithm / Pattern

不打扰是莪最后的温柔 提交于 2019-12-02 04:56:19

问题


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();


来源:https://stackoverflow.com/questions/7452412/android-screen-size-algorithm-pattern

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!