Android:维护图像资源大小
我知道关于这个主题有很多问题,但我尝试搜索大量“android dpi”帖子并阅读在线文档,但没有找到我所遇到的确切问题。
如果我想创建一个按钮(或任何图像资源),该按钮将在所有设备的屏幕上占用相同的空间 - 我该怎么做?
我知道 Android 会查看特定的屏幕尺寸和 dpi 等,但我不太明白特定图像应该有多大才能在任何屏幕上正确显示。
假设我对可绘制和布局文件夹进行了以下设置(应用程序仅是横向):
res/drawable-ldpi/button.png (120 dpi)
res/drawable-mdpi/button.png (160 dpi)
res/drawable-hdpi/button.png (240 dpi)
res/layout-small/main.xml - button is set to 100dp
res/layout-normal/main.xml - button is set to 100dp
res/layout-large/main.xml - button is set to 100dp
假设我有一个按钮,我希望在任何屏幕上都将其设置为 100dp。按照 3:4:6 比例规则,如果我有一个 mdpi 密度为 100 像素宽的按钮资源,我应该为 75 像素的 ldpi 密度制作另一个按钮,并为 150 像素的 hdpi 密度制作另一个按钮。这是正确的吗?我是否要以 120dpi 的分辨率创建 75px 的按钮,以 160dpi 的分辨率创建 100px 的按钮,以 240dpi 的分辨率创建 150px 的按钮,然后在所有三个布局文件中只写 android:layout_width="100dp" ?或者我是否将所有三个按钮在不同的 dpi 下设置为 100px?
现在如果我修补了按钮 9 会怎么样?我是否必须以 120dpi、160dpi 和 240dpi 的分辨率创建按钮 3 次,所有尺寸均为 100px?或者我只是创建一个 100px 和 160dpi 的 9 修补按钮并将其放置在 mdpi 可绘制文件夹中?
在线文档对我来说有点混乱,所以我正在寻找一个简化的答案。
这是适用于 Android 版本 1.6+(不是 3.2 等的新内容)
感谢您的帮助...我只是想要一个按钮在 3.7 英寸屏幕上占据与 7 英寸屏幕相同的空间,并且不模糊!
I know there are tons of questions on this topic, but I tried searching through tons of "android dpi" posts and read the online documentation but haven't found the exact question that I have.
If I want to create a button (or any image resource) that will take up the same space on the screen on all devices - how do I do that?
I understand that there are specific screen sizes and dpi's that Android looks at etc. but I don't quite understand how big a specific image should be to look right on any screen.
Say that I have the following setup for my drawable and layout folders (Application is only landscape):
res/drawable-ldpi/button.png (120 dpi)
res/drawable-mdpi/button.png (160 dpi)
res/drawable-hdpi/button.png (240 dpi)
res/layout-small/main.xml - button is set to 100dp
res/layout-normal/main.xml - button is set to 100dp
res/layout-large/main.xml - button is set to 100dp
Say I have a button that I want to be 100dp on any screen. Following the 3:4:6 scale ratio rule it seems that if I have a button resource that's 100px wide for the mdpi density I should make another button for the ldpi density that's 75px, and one for the hdpi density that's 150px. Is that correct? Do I create the 75px button at 120dpi, the 100px button at 160dpi and the 150px button at 240dpi and then just say android:layout_width="100dp" in all three layout files? Or do I make all three buttons 100px at the different dpi's?
Now what if I have that button 9 patched. Do I have to create the button three times at 120dpi, 160dpi and 240dpi all of which are 100px in size? Or do I just create one 9 patched button at 100px and 160dpi and place it in the mdpi drawable folder?
The online documentation is a bit confusing for me so I'm looking for a simplified answer.
This is for Android versions 1.6+ (Not the new stuff for 3.2 etc)
Thanks for your help ... I just want a button to take up the same space on a 3.7" screen as a 7" screen and not be blurry!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你把这个排序了吗?据我所知,根据我的经验,在 Photoshop 中创建图像时不要更改 dpi。我总是把我的标准设置为 72,我想。无论如何,我认为将其设置为一百万并不重要。
您提到的 px 值对于 lm 和 h 是正确的。
不过,我会保留文件夹名称中的 mdpi 限定符;我不确定 xhdpi 设备上是否存在没有 xhdpi 限定符的可绘制文件夹和没有限定符的文件夹的协议。
工作流程:
以 75、100 和 150 创建图像。
将 75 放入 ldpi 文件夹中,将 100 放入无限定符的drawables 文件夹中,将 150 放入 hdpi 中。
保持您现在的布局不变,即 100dp。它们应该正确缩放。也就是说,如果您使用全分辨率,我认为您不需要指定图像大小。
总是欢迎针对我的答案中的不准确之处发表评论,即使是多年以后。
Did you sort this? Afaik, and from my experience, don't change the dpi when creating the images in say, Photoshop. I always left mine at standard which is 72 I think. Regardless, I don't think it matters if you set it to a million.
The px values you mentioned are correct for l m and h.
I would leave the mdpi qualifier off the folder name though; I'm unsure of the protocol if on an xhdpi device there is no drawable folder with no xhdpi qualifier and no folder without qualifiers.
Workflow:
Create your images at 75, 100 and 150.
Put 75 in ldpi folder, 100 in drawables folder with no qualifier, and 150 in hdpi.
Leave your layouts as they are now, with the 100dp. They should scale correctly. That said, I don't think you need to specify the image size if you're using the full resolution.
Comments to address inaccuracies in my answer are always welcome, even years down the line.