Calculate screen size in android

社会主义新天地 提交于 2019-12-25 15:19:23

问题


I have traveled all over the internet looking for a way to do something that I thought would be very basic. Bottom line is: I have an android UI that I have designed. It consists of buttons that are placed along the middle of the screen. This is sort of a menu screen. These buttons need to be in the SAME location but should just increase or decrease in size in relation to the physical size of the screen. Basically, if i have a button on a screen that is 1px (or dip; dip is what i currently use in my application) X 1px, then, if i double the screen size, the button should auto-format to 2px X 2px. I have done the math on my application. The button that I have is about 225/854 down the screen (this comes out to be about 26.44% from the top.) All I want to do is make that button come down the same amount. Say I reduced the size of the screen to 500, the ratio should stay the same.

Example math work:

(225 dip/854 px)*100=26.44%

so if I reduce the screen size to 500px, the dip should be as follows.

(225dip/854px)*500px = 131.733021077283372 dip

Is this the best way to go about scaling my buttons? If so, how do I tell my application to calculate the correct number of dip that the button should be placed at?

If you are still confused (sorry!), here is a key to looking at that work.

225dip = how far the button comes down from the top of the screen. 854px = physical size (in pixels) of the screen 131.733021077283372 dip = new number of dip if screen is reduced to 500px physical size


回答1:


If you design your interface using dip (or dp) and sp units (for fonts) you can let Android do the scaling for you. Although maybe not pixel perfect on every device, your app will certainly look the same on many devices in the correct scale.

Supporting multiple screens on http://d.android.com describes this...




回答2:


You can use the WindowManager to get the screen size. In your activity:

getWindow().getWindowManager().getDefaultDisplay().getWidth();

or getHeight() in your case. Once you have that, just do your math, and then:

ViewGroup.LayoutParams buttonLayout = yourButton.getLayoutParams();

buttonLayout.height = 42; // Set your height

buttonLayout.y = 42; //Set distance from top of the screen

and so on. Once done:

yourButton.setLayoutParams(buttonLayout);

Let me know if this works.




回答3:


I am a bit new to android, although, in the game I am creating I found that it is easiest to allow android to determine the dpi(dots per inch) of the screen and choose my images accordingly.

For example, my games res folder has folders listed as drawable, drawable-hdpi, and more. These folder are here for you to put the corresponding images in for the type of screen.

In the android manifest you can list what screen dpi you support and those folders will be used correctly.

Hope this helps!

EDIT: I would definitely check out this doc page

http://developer.android.com/guide/practices/screens_support.html



来源:https://stackoverflow.com/questions/5888942/calculate-screen-size-in-android

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