Android中计算屏幕尺寸

发布于 2024-11-05 03:12:21 字数 650 浏览 4 评论 0原文

我走遍了整个互联网,寻找一种方法来做一些我认为非常基本的事情。底线是:我有一个我设计的 Android 用户界面。它由沿屏幕中间放置的按钮组成。这是一个菜单屏幕。这些按钮需要位于同一位置,但应根据屏幕的物理尺寸增加或减小尺寸。基本上,如果我的屏幕上有一个按钮,尺寸为 1 像素(或倾角;倾角是我当前在应用程序中使用的尺寸)X 1 像素,那么,如果我将屏幕尺寸加倍,按钮应自动格式化为 2 像素 X 2 像素。我已经对我的申请进行了数学计算。我的按钮位于屏幕下方约 225/854(距离顶部约 26.44%)。我想做的就是让该按钮向下移动相同的距离。假设我将屏幕尺寸缩小到 500,比例应该保持不变。

示例数学工作:

(225 倾角/854 像素)*100=26.44%

因此,如果我将屏幕尺寸减小到 500 像素,倾角应如下。

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

这是缩放按钮的最佳方法吗?如果是这样,我如何告诉我的应用程序计算按钮应放置的正确下沉次数?

如果您仍然感到困惑(抱歉!),这里是查看该工作的关键。

225dip = 按钮从屏幕顶部下降的距离。 854px = 屏幕的物理尺寸(以像素为单位) 131.733021077283372 倾角 = 如果屏幕缩小到 500 像素物理尺寸,则新的倾角次数

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

毁梦 2024-11-12 03:12:21

您可以使用 WindowManager 来获取屏幕尺寸。在您的活动中:

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

或在您的情况下使用 getHeight() 。一旦你有了这个,只需做你的数学,然后:

ViewGroup.LayoutParams buttonLayout = yourButton.getLayoutParams();

buttonLayout.height = 42; // Set your height

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

等等。完成后:

yourButton.setLayoutParams(buttonLayout);

让我知道这是否有效。

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.

℡寂寞咖啡 2024-11-12 03:12:21

如果您使用dip(或dp)和sp单位(用于字体)设计界面,您可以让Android为您进行缩放。尽管在每台设备上可能不是像素完美,但您的应用程序在许多设备上以正确的比例肯定会看起来相同。

支持多个屏幕 http://d.android.com 描述了这个...

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...

喵星人汪星人 2024-11-12 03:12:21

我对 android 有点陌生,不过,在我创建的游戏中,我发现最简单的方法是让 android 确定屏幕的 dpi(每英寸点数)并相应地选择我的图像。

例如,我的游戏 res 文件夹中列出了可绘制、可绘制-hdpi 等文件夹。这些文件夹供您放置与屏幕类型相对应的图像。

在 Android 清单中,您可以列出您支持的屏幕 dpi 以及将正确使用这些文件夹。

希望这有帮助!

编辑:我肯定会查看此文档页面

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文