Android 应用骨架支持多屏
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12242111/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Application Skeleton to support multiple screens
提问by Mohammed Azharuddin Shaikh
As we know Android coming with various device which having different Features, Resolution, and Screen-size so while developing an Application which support multiple (small and big) screen there is an obstacle of size and layout.
众所周知,Android 带有各种具有不同功能、分辨率和屏幕尺寸的设备,因此在开发支持多个(小和大)屏幕的应用程序时,存在尺寸和布局方面的障碍。
This leads to different combinations of screen sizes, resolutions, and DPIsand creates quite a challenge when designing and developing for Android devices. While some other Manufacturer (non Android) have different resolutions and DPI, they share the same screen size and the resolutions follow the same aspect ratio. Therefore, an image can be created to fit the non Android devices.
这导致屏幕尺寸、分辨率和 DPI 的不同组合,并在为 Android 设备设计和开发时带来了相当大的挑战。虽然其他一些制造商(非 Android)具有不同的分辨率和 DPI,但它们共享相同的屏幕尺寸并且分辨率遵循相同的纵横比。因此,可以创建适合非 Android 设备的图像。
My question is that is there a proper flow or architecture that one should follow to meet the requirement?
我的问题是,是否应该遵循适当的流程或架构来满足要求?
Remember we do have Tablets of different Size and Resolution.
请记住,我们确实有不同尺寸和分辨率的平板电脑。
I'm aware that Android Developercontains this information but my view is from implementation.
我知道Android Developer包含此信息,但我的观点来自实施。
From my knowledge what I understood is that for designing Android graphics even Programmer must know the designing concept.
据我所知,为了设计Android图形,即使是程序员也必须了解设计理念。
回答by Mohammed Azharuddin Shaikh
Finally created a structure which handle layouts and icon for multiple screen.
最后创建了一个处理多屏幕布局和图标的结构。
Android generalises device displays into categories based on two parameters:
Android 将设备显示概括为基于两个参数的类别:
- Screen size, the physical size of the display (measured diagonally)
- Screen density, the physical pixel density of the display (in pixels-per-inch, or ppi)`
- 屏幕尺寸,显示器的物理尺寸(对角线测量)
- 屏幕密度,显示器的物理像素密度(以每英寸像素或 ppi 为单位)`
To determine screen size & density quickly, please install "What's my Size" app for Android.
要快速确定屏幕尺寸和密度,请安装适用于 Android 的“我的尺寸是多少”应用程序。
Screen size
屏幕尺寸
Android defines four generalised screen sizes:
Android 定义了四种通用的屏幕尺寸:
Qualifier Size
small ~3 inches (approx)
normal ~4 inches (approx)
large Exceeds 4 inches
xlarge Exceeds 7 inches
- Most phones are classified as small or normal (roughly 3 to 4 inches diagonally). But now, there are many phones with large screen such as Galaxy S4, HTC One, Xperia Z
- A small tablet like the Samsung Galaxy Tab is classified as large (larger than 4 inches)
- Extra-large applies to large devices, for example large tablets
- 大多数手机被归类为小型或普通手机(对角线大约 3 到 4 英寸)。但是现在有很多大屏手机,比如Galaxy S4、HTC One、Xperia Z
- 像三星 Galaxy Tab 这样的小型平板电脑被归类为大型(大于 4 英寸)
- 超大适用于大型设备,例如大型平板电脑
Android defines four generalised screen densities:
Android 定义了四种通用的屏幕密度:
Qualifier Description Nominal value
ldpi low density 120 ppi
mdpi medium density 160 ppi
hdpi high density 240 ppi
xhdpi extra high density 320 ppi
Typically:
通常:
- screen size has most impact on your app layouts
- screen density has most impact on your image and graphic resources
- 屏幕尺寸对您的应用布局影响最大
- 屏幕密度对您的图像和图形资源影响最大
It is listed herethe percentage difference of device screen
这里列出了设备屏幕的百分比差异
- Ldpi- 75%
- Mdpi- 100% (base according to Android developer site)
- Hdpi- 150%
- XHdpi- 200%
- Ldpi- 75%
- Mdpi- 100%(基于 Android 开发者网站)
- Hdpi- 150%
- XHdpi- 200%
But as we know now most of device coming with 480X800so I'm consider this as based device, so our new calculation will like this
但正如我们现在所知,大多数设备都带有480X800,所以我认为这是基于设备,所以我们的新计算会像这样
- Ldpi- 50%
- Mdpi- 66.67%
- Hdpi- 100%
- XHdpi- 133.33%
- Ldpi- 50%
- Mdpi- 66.67%
- Hdpi- 100%
- XHdpi- 133.33%
which means that first icon and design will be created for 480X800only and then for rest ones(i.e. Ldpi, Mdpi, Xhdpi).
这意味着第一个图标和设计将只为480X800创建,然后为其余的(即 Ldpi、Mdpi、Xhdpi)创建。
There are images which are common for all layout and must uniform in color and shape(no complex shape, no curve) so for this kind of image we are creating 9patch
which to be put in “drawable(no-suffix)” folder. To create 9Patch image you can either use DrawNinePatchor BetterNinePatch
有些图像对于所有布局都是通用的,并且必须在颜色和形状上统一(没有复杂的形状,没有曲线),因此对于这种我们正在创建的图像9patch
,将其放入“drawable(无后缀)”文件夹中。要创建 9Patch 图像,您可以使用DrawNinePatch或BetterNinePatch
Now just rename your images based on Android's standards and complete your application with hdpi
and then just take drawable-hdpi
folder and Open Adode Photoshop(recommended)
create Actionof multiple size(just change the size according to percentage ratio) once Action created for all size then just do Batch Automateand give source(drawable-hdpi) and destination(drawable-ldpi, drawable-mdpi, drawable-xdpi).
现在只需根据 Android 的标准重命名您的图像并完成您的应用程序,hdpi
然后只需获取drawable-hdpi
文件夹并打开Adode Photoshop(推荐)创建多个大小的动作(只需根据百分比更改大小)为所有大小创建动作然后就做批量自动化并提供源(drawable-hdpi)和目标(drawable-ldpi、drawable-mdpi、drawable-xdpi)。
The reason I insist you to use Photoshop because it will resize automatically your image with Actions and one more plus point is that you need not to rename the file(it will assign same name as original one).
我坚持您使用 Photoshop 的原因是它会使用 Actions 自动调整您的图像大小,另外一个优点是您无需重命名文件(它会分配与原始文件相同的名称)。
once you completed with creation of all images, refresh your project and test it.
完成所有图像的创建后,刷新项目并进行测试。
Sometimes there may be possibility that the layout which support screen(xhdpi, hdpi, mdpi) may be get cut in small screen(ldpi) so for handling this just create separate Layout folder(layout-small) for it and add ScrollView
(mostly). Thats it.
有时,支持屏幕(xhdpi,hdpi,mdpi)的布局可能会在小屏幕(ldpi)中被剪切,因此为了处理这个问题,只需为其创建单独的布局文件夹(layout-small)并添加ScrollView
(大部分)。就是这样。
TabletTablets are categorized into two size.
平板电脑平板电脑分为两种尺寸。
- 7"(1024X(600-48(navigation bar))) = 1024X552 (drawable-large)
- 10"(1280X(800-48(navigation bar))) = 1280X752 (drawable-xlarge)
- 7"(1024X(600-48(导航栏))) = 1024X552 (drawable-large)
- 10"(1280X(800-48(导航栏))) = 1280X752 (drawable-xlarge)
In this we need to create image for both the screen and just put them accordingly
在这个我们需要为两个屏幕创建图像并相应地放置它们
So all in all we will have this folder in our application to support multiple screen.
所以总而言之,我们将在我们的应用程序中使用这个文件夹来支持多屏幕。
drawable
drawable-ldpi
drawable-mdpi
drawable-hdpi
drawable-xhdpi
drawable-large
drawable-xlarge
will be more qualifier combination with Screen size and Screen density
将更多的限定符组合与 Screen size and Screen density
drawable-large-ldpi
drawable-large-mdpi
drawable-large-hdpi
drawable-large-xhdpi
more qualifier with Screen density and Version
更多限定符 Screen density and Version
drawable-ldpi-v11
drawable-mdpi-v11
drawable-hdpi-v11
drawable-xhdpi-v11
and more qualifier with Screen size and Version
和更多的限定词 Screen size and Version
drawable-large-v11
drawable-xlarge-v11
and more qualifier with Smallest width concept(SW)
和更多的限定词 Smallest width concept(SW)
drawable-sw???dp
Further more in Android V3.0 Honeycomb they introduced new concept of SW(smallest width)
in which device are categorized into screen width, so if we are creating a folder named drawable-sw360dp
then the device with 720dp(either width or height) will use resource from the this folder.
此外,在 Android V3.0 Honeycomb 中,他们引入SW(smallest width)
了将设备分类为屏幕宽度的新概念,因此如果我们创建一个名为drawable-sw360dp
720dp(宽度或高度)的设备的文件夹,将使用该文件夹中的资源。
for example to find the Samsung Galaxy S3
dpto suffix to drawable-sw?dp
With reference of DP Calculation, If you want to support your layout or drawable to S3 then the calculation says
例如找到Samsung Galaxy S3
dp后缀到drawable-sw?dp
参考DP Calculation,如果你想支持你的布局或可绘制到 S3 那么计算说
px= Device's width = 720
dpi= Device's density= 320
px= 设备的宽度 = 720
dpi= 设备的密度 = 320
formula given
给出的公式
px = dp * (dpi / 160)
interchanging formula because we have px's value
交换公式,因为我们有 px 的值
dp = px / (dpi / 160)
now putting value,
现在投入价值,
dp= 720 / (320/160);
dp=360.
so drawable-sw360dp
will do the job
所以drawable-sw360dp
会做这项工作
Get you Device configuaration from GsmArenaSameway you can also create folder according to Device's Android API version i.e. drawable-hdpi-v11` so the device which is having API11 and it is Hdpi then it will use this resources.
从GsmArenaSameway获取设备配置,您还可以根据设备的 Android API 版本创建文件夹,即 drawable-hdpi-v11`,因此具有 API11 且为 Hdpi 的设备将使用此资源。
Additional Tips:
附加提示:
Use relative layouts, dp, sp, and mm
dp units- device independent pixels normalised to 1 physical pixel on a 160 ppi screen i.e. medium density. Scaled at runtime. Use for screen element dimensions
sp units- scaled pixels, specified as floating point values, based on dp units but additionally scaled for the user's font-size preference setting. Scaled at runtime. Use for font sizes
you should always use RelativeLayout for layouts; AbsoluteLayout is deprecated and should not be used.
Use appropriate image formats - PNG versus JPEG
Android "prefers" PNG for bitmap image files, "accepts" JPEG, and "discourages" GIF.
However, PNG and JPEG are not equivalents. They have different quality trade offs, and PNG is not always best:
JPEGcan offer up to 50% file-size reductions over PNG, which is significant if your app is image-intensive
A higher quality "lossy" JPEG may look better than a highly compressed "lossless" PNG, for the same file size
Add labels to your images and graphics for debugging
Use the supports-screens element
Configure your emulators with real device values
Conventionally, desktop systems display at 72ppi (Mac), or 96ppi (Windows, Linux). Compared with mobile, desktop displays are always low density.
Always configure your Android emulators to mimic real device values, and always set them to scale to emulate device density.
In Eclipse, it's easy to create multiple emulators (from the Eclipse menu bar, select Window > AVD Manager > New) configured with values for real devices:
Name the emulator for the real device it's emulating Specify Resolution, don't use Built-in generic sizes Set the device density to match the real device (in the Hardware pane set Abstracted LCD Property to the real density, always an integer value)
When you launch the device, always select Scale display to real size, and type in the real screen dimension in inches.
If you don't set the device density, the emulator defaults to low density, and always loads ldpi-specific resources. Resolution (pixel dimensions) will be correct, but your density-dependent image resources will not display as intended.
Of course, nothing you do will reproduce higher density image quality on a lower density desktop display.
使用相对布局,dp、sp 和 mm
dp 单位- 在 160 ppi 屏幕(即中等密度)上标准化为 1 个物理像素的设备独立像素。在运行时缩放。用于屏幕元素尺寸
sp 单位- 缩放像素,指定为浮点值,基于 dp 单位,但根据用户的字体大小首选项设置进行了额外缩放。在运行时缩放。用于字体大小
您应该始终使用 RelativeLayout 进行布局;AbsoluteLayout 已弃用,不应使用。
使用适当的图像格式 - PNG 与 JPEG
Android "prefers" PNG for bitmap image files, "accepts" JPEG, and "discourages" GIF.
但是,PNG 和 JPEG 不是等价的。它们有不同的质量权衡,PNG 并不总是最好的:
与 PNG 相比,JPEG最多可以减少 50% 的文件大小,如果您的应用程序是图像密集型的,这一点非常重要
对于相同的文件大小,更高质量的“有损”JPEG 可能比高度压缩的“无损”PNG 看起来更好
为图像和图形添加标签以进行调试
使用 support-screens 元素
使用真实设备值配置您的模拟器
通常,桌面系统以 72ppi(Mac)或 96ppi(Windows、Linux)显示。与移动设备相比,桌面显示器始终是低密度的。
始终将您的 Android 模拟器配置为模拟真实设备值,并始终将它们设置为缩放以模拟设备密度。
在 Eclipse 中,很容易创建多个仿真器(从 Eclipse 菜单栏,选择Window > AVD Manager > New)配置真实设备的值:
为它正在模拟的真实设备命名模拟器指定分辨率,不要使用内置通用尺寸设置设备密度以匹配真实设备(在硬件窗格中将抽象 LCD 属性设置为真实密度,始终为整数值)
启动设备时,请始终选择将显示缩放到实际大小,然后输入以英寸为单位的实际屏幕尺寸。
如果不设置设备密度,模拟器默认为低密度,并且总是加载 ldpi 特定的资源。分辨率(像素尺寸)将是正确的,但与密度相关的图像资源不会按预期显示。
当然,您所做的任何事情都不会在较低密度的桌面显示器上再现较高密度的图像质量。
Here is the Data collected during a 7-day period ending on October 1, 2012. To see the latest statistic about Android platform version, go to here
以下是截至 2012 年 10 月 1 日的 7 天期间收集的数据。要查看有关 Android 平台版本的最新统计数据,请转到此处
Based on Screen Size
基于屏幕尺寸
Based on Screen Density
基于屏幕密度
回答by Alok Vaish
Designers should create base designs of
设计师应该创建基础设计
base size of mdpi devices * density conversion factor of highest supported density bucket
size.Base screen size is 320 X 480 px and density buckets are as follows:
base size of mdpi devices * density conversion factor of highest supported density bucket
size.Base 屏幕尺寸为 320 X 480 px,密度桶如下:
- ldpi: 0.75
- mdpi: 1.0 (base density)
- hdpi: 1.5
- xhdpi: 2.0
- xxhdpi: 3.0
- xxxhdpi: 4.0
- 低密度脂蛋白:0.75
- mdpi:1.0(基本密度)
- 分辨率:1.5
- xhdpi:2.0
- xxhdpi:3.0
- xxxhdpi:4.0
And to tackle extra available space on Android devices should use stretchable components in both the directions (horizontally and vertically). Detailed info is available here:
为了解决 Android 设备上额外的可用空间,应该在两个方向(水平和垂直)上使用可伸缩组件。详细信息可在此处获得:
http://vinsol.com/blog/2014/11/20/tips-for-designers-from-a-developer/
http://vinsol.com/blog/2014/11/20/tips-for-designers-from-a-developer/