如何确定 Android 基准绘图的像素尺寸?
简短问题
我的 Android XML 布局中有一个 50dip x 40dip 按钮。我需要放置在 mdpi 资源文件夹中的可绘制对象的像素尺寸是多少?
长问题
假设我有一个 50dip x 40dip 按钮。
<Button
android:layout_width="50dip"
android:layout_height="40dip"
android:background="@drawable/someImage" />
我对手机上这个按钮的物理尺寸感到满意。我的问题是,当我在 Photoshop 中设计 someImage.png 时,如何决定图像的像素尺寸?目前,我只是将其制作为一个巨大的 500px x 400px 图像,并将其放置在默认的可绘制资源文件夹中,因为它在我测试过的所有手机和平板电脑上都运行良好。
支持多屏幕的参考页面没有告诉我如何决定 像素尺寸。
问题 #1 - 基线图形
该图仅告诉我需要放入的图形的相对尺寸每个可绘制资源文件夹。但是,它没有告诉我制作基线 mdpi 图形所需的像素尺寸,所有其他图形都相对于此。
问题#2 - 屏幕密度碎片
与密度无关的像素相当于屏幕上的一个物理像素。 160 dpi 屏幕,这是系统假设的基线密度 对于“中等”密度屏幕。在运行时,系统透明地 根据实际情况,根据需要处理 dp 单位的任何缩放 使用中的屏幕密度。 dp单位与屏幕的换算 像素很简单:px = dp * (dpi / 160)。例如,在 240 dpi 上 屏幕上,1 dp 等于 1.5 个物理像素。您应该始终使用 dp 定义应用程序 UI 时的单位,以确保正确显示 您的用户界面在不同密度的屏幕上。
这个小花絮告诉我,160dpi 屏幕上 50dip 宽的图形将显示为 50px 宽。太棒了,你说。我应该在 Photoshop 中将 mdpi 图形(160dpi 属于 mdpi)设置为 50px 宽。嗯,事情没那么简单,因为并非所有 mdpi 手机都是 160dpi。如果我妈妈的mdpi手机是167dpi怎么办?然后我的 50dip 宽图形在她的手机上显示为 52.1875px,这可能会导致一些奇怪的模糊伪影。那我该怎么办呢?我应该根据我母亲的 167dpi 手机来模拟我的基线 mdpi,还是应该根据完美的实验室 160dpi 手机来模拟它,而现实世界中可能根本不存在?
Short Question
I have a 50dip by 40dip button in my Android XML layout. What are the pixel dimensions of the drawable that I need to place in my mdpi resource folder?
Long Question
Let's say I have a 50dip x 40dip button.
<Button
android:layout_width="50dip"
android:layout_height="40dip"
android:background="@drawable/someImage" />
I am satisfied with the physical size of this button on my phone. My question is when I'm designing someImage.png in Photoshop, how do I decide on the pixel dimensions of the image? Currently, I'm just making it a gigantic 500px by 400px image and placing it in the the default drawable resource folder because it's working okay across all the phones and tablets I've tested on.
The reference page on supporting multiple screens does not tell me how to decide the
pixel dimensions.
Problem #1 - Baseline graphic
This diagram only tells me the relative dimensions of the graphics I need to put into each drawable resource folder. However, it does not tell me the pixel dimension I need to make my baseline mdpi graphic, from which all other graphics are relative to.
Problem #2 - screen density fragmentation
The density-independent pixel is equivalent to one physical pixel on a
160 dpi screen, which is the baseline density assumed by the system
for a "medium" density screen. At runtime, the system transparently
handles any scaling of the dp units, as necessary, based on the actual
density of the screen in use. The conversion of dp units to screen
pixels is simple: px = dp * (dpi / 160). For example, on a 240 dpi
screen, 1 dp equals 1.5 physical pixels. You should always use dp
units when defining your application's UI, to ensure proper display of
your UI on screens with different densities.
This little tidbit tells me that a 50dip wide graphic on a 160dpi screen will appear to be 50px wide. So great, you say. I should make my mdpi graphic (160dpi falls under mdpi) 50px wide in Photoshop. Well, it's not that simple, because not all mdpi phones are 160dpi. What if my mother's mdpi phone is 167dpi? Then my 50dip wide graphic would appear as 52.1875px on her phone, which would likely cause some weird blurring artifacts. So what am I supposed to do? Should I model my baseline mdpi off of my mother's 167dpi phone or should I model it off of the perfect laboratory 160dpi phone which maybe doesn't even exist in the real world?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
当谈到问题#2时...
您应该针对完美的实验室 160dpi 手机进行建模。这几乎就是那篇文章想要告诉我们的内容。我的理由是 ldpi、mdpi 等是“广义”密度,Android 操作系统的图形引擎有责任正确补偿任何给定设备的实际 dpi。除了强制设备制造商使用授权的屏幕组件(这不会发生)之外,确实没有其他方法可以做到这一点。
至于问题#1,我会查看按钮的宽度:高度比,并根据该尺寸选择实际像素尺寸但大于“模型”50 x 40像素。宽:高的比例是 5:4,所以我会选择可被 5 整除的宽度,但大于 50 或者 60,可以方便地被 5 和 4 整除。这意味着将按钮图像创建为 60 x 48 px,两者都当出于 ldpi 目的乘以 0.75 时,给出预期值 (45 x 36 px)。
When it comes to problem #2...
You should model it aiming at the perfect laboratory 160dpi phone. This is pretty much what that article is trying to tell us. My reasoning is that ldpi, mdpi etc are 'generalised' densities and it is the responsibility of the Android OS's graphics engine to correctly compensate for actual dpi of any given device. There really isn't any other way of doing it other than forcing device manufacturers to use authorised screen components (which isn't going to happen).
As for problem #1, I would look at the width:height ratio of your button and pick actual pixel dimensions based on that but larger than the 'model' 50 x 40 px. The W:H ratio is 5:4 so I'd go for a width divisible by 5 but larger than 50 perhaps 60 which is conveniently divisible by 5 and 4. This would mean creating your button image as 60 x 48 px both of which when multiplied by 0.75 for ldpi purposes give exect values (45 x 36 px).
我所有的图形都使用 320x480 作为总屏幕尺寸。在我准备好导出其他密度图形之前,像素密度并没有真正发挥作用。最终结果是 480*1.5 等等。
另外一点,当您执行 50dip x 40dip 时,您真正所说的是使用 320px x 480px 基线的 50px x 40px。 Android 会为你在一定程度上缩放图像,这样你所做的事情看起来就不会那么糟糕。在这种情况下,您应该做的是为 mdpi 制作一个 50px x 40px 按钮,为 hdpi 制作一个 75px x 60px(这些是 x 1.5)等。
I base all my graphics off using 320x480 as the total screen size. Pixel density does not really play a role until I'm ready exporting the other density graphics. That ends up being 480*1.5 and etc.
To make another point, when you are doing 50dip x 40dip what you are really saying is 50px x 40px using a 320px x 480px baseline. Android will scale images to a certain extent for you so what you were doing does not look half bad. What you should do in this case is make a 50px x 40px button for mdpi, and make a 75px x 60px(these are x 1.5) for hdpi etc.
我不创建进入我们的应用程序的资产(设计师做),但我花了很多时间修复它们,以便它们可以在不同的设备上使用。
我建议这样:
为 mdpi 320x480 设备制作布局设计(线框),为您要支持的最高分辨率制作漂亮的设计。然后考虑一下 - 哪些资源需要拉伸、最小和最大尺寸等以支持不同的屏幕。不要只是按照设计中出现的方式对资源进行切片, .9 修补它们,优化它们。
使用高分辨率设计中的资源,让 Android 为您缩小它们 - 您已经布置了所有内容以适合 mdpi 设备(不支持 ldpi...urggghhhh...)。按钮背景不应该是固定的大小,它应该是具有最小大小的 9 修补 png - 内容(文本或图标)/可用空间/xml 布局将决定最终大小。
我可能只是胡言乱语。
I don't create the assets that go into our apps (designers do), but I spend a lot of time fixing them so that they are usable across different devices.
I'd suggest something like this:
Make your layout designs (wireframes) for a mdpi 320x480 device, make your pretty designs for the highest res you're going to support. Then think about it - what assets need to stretch, minimum and maximum sizes etc to support different screens. Don't just slice the assets as they appear in your design, .9 patch them, optimise them.
Use the assets from your high res designs and let Android scale them down for you - you've laid out everything to fit on mdpi devices (don't support ldpi... urggghhhh....). A button background shouldn't be a set size, it should be a 9 patched png with min sizes - the content (text or icon) / available space / xml layout will determine the final size.
I might just be blabbering.