多分辨率适配

Supporting Different Screens

上一课下一课

This lesson teaches you to

  1. Create Different Layouts
  2. Create Different Bitmaps

You should also read

Android categorizes device screens using two general properties: size and density. You should expect that your app will be installed on devices with screens that range in both size and density. As such, you should include some alternative resources that optimize your app’s appearance for different screen sizes and densities.

  • There are four generalized sizes: small, normal, large, xlarge
  • And four generalized densities: low (ldpi), medium (mdpi), high (hdpi), extra high (xhdpi)

To declare different layouts and bitmaps you‘d like to use for different screens, you must place these alternative resources in separate directories, similar to how you do for different language strings.

Also be aware that the screens orientation (landscape or portrait) is considered a variation of screen size, so many apps should revise the layout to optimize the user experience in each orientation.

Create Different Layouts



To optimize your user experience on different screen sizes, you should create a unique layout XML file for each screen size you want to support. Each layout should be saved into the appropriate resources directory, named with a -<screen_size> suffix. For example, a unique layout for large screens should be saved underres/layout-large/.

Note: Android automatically scales your layout in order to properly fit the screen. Thus, your layouts for different screen sizes don‘t need to worry about the absolute size of UI elements but instead focus on the layout structure that affects the user experience (such as the size or position of important views relative to sibling views).

For example, this project includes a default layout and an alternative layout for large screens:

MyProject/
    res/
        layout/
            main.xml
        layout-large/
            main.xml

The file names must be exactly the same, but their contents are different in order to provide an optimized UI for the corresponding screen size.

Simply reference the layout file in your app as usual:

@Override protected void onCreate(Bundle savedInstanceState) {     super.onCreate(savedInstanceState);     setContentView(R.layout.main);}

The system loads the layout file from the appropriate layout directory based on screen size of the device on which your app is running. More information about how Android selects the appropriate resource is available in theProviding Resources guide.

As another example, here‘s a project with an alternative layout for landscape orientation:

MyProject/
    res/
        layout/
            main.xml
        layout-land/
            main.xml

By default, the layout/main.xml file is used for portrait orientation.

If you want to provide a special layout for landscape, including while on large screens, then you need to use both the large and land qualifier:

MyProject/
    res/
        layout/              # default (portrait)
            main.xml
        layout-land/         # landscape
            main.xml
        layout-large/        # large (portrait)
            main.xml
        layout-large-land/   # large landscape
            main.xml

Note: Android 3.2 and above supports an advanced method of defining screen sizes that allows you to specify resources for screen sizes based on the minimum width and height in terms of density-independent pixels. This lesson does not cover this new technique. For more information, read Designing for Multiple Screens.

Create Different Bitmaps



You should always provide bitmap resources that are properly scaled to each of the generalized density buckets: low, medium, high and extra-high density. This helps you achieve good graphical quality and performance on all screen densities.

To generate these images, you should start with your raw resource in vector format and generate the images for each density using the following size scale:

  • xhdpi: 2.0
  • hdpi: 1.5
  • mdpi: 1.0 (baseline)
  • ldpi: 0.75

This means that if you generate a 200x200 image for xhdpi devices, you should generate the same resource in 150x150 for hdpi, 100x100 for mdpi, and 75x75 for ldpi devices.

Then, place the files in the appropriate drawable resource directory:

MyProject/
    res/
        drawable-xhdpi/
            awesomeimage.png
        drawable-hdpi/
            awesomeimage.png
        drawable-mdpi/
            awesomeimage.png
        drawable-ldpi/
            awesomeimage.png

Any time you reference @drawable/awesomeimage, the system selects the appropriate bitmap based on the screen‘s density.

Note: Low-density (ldpi) resources aren’t always necessary. When you provide hdpi assets, the system scales them down by one half to properly fit ldpi screens.

For more tips and guidelines about creating icon assets for your app, see the Iconography design guide.

时间: 2024-10-08 09:29:50

多分辨率适配的相关文章

【转】Unity3d + NGUI 的多分辨率适配

原文地址:http://www.cnblogs.com/cqgreen/p/3348154.html 一.当下移动设备的主流分辨率(数据来自“腾讯分析移动设备屏幕分辨率分析报告”) 1.1 iOS设备的分辨率主要有: 宽 高 宽高比 960 640 1.5 1136 640 1.775 1024 768 1.3333 2048 1536 1.3333 Android设备的分辨率则相对纷杂,主流的分辨率有: 宽 高 宽高比 800 480 1.6667 854 480 1.7792 1280 72

Android多分辨率适配经验总结

??Android多分辨率适配是一件很有意义但是比较麻烦的事情,网上有很多关于多分辨率适配的文章,多数文章讲解的都是整个APP的图片比较规则,可以将图片做成9图来完成多分辨率适配,但是对于一些游戏类应用(这里说的游戏没有使用游戏引擎).低龄儿童应用,APP中有很多花哨的图片,这种APP的图片显然无法做成9图,在网上查了很多资料始终没有比较理想的解决方案,结合自己最近做的项目介绍一下针对这种情况下的多分辨率适配: 为了减少UI的工作量,一个APP只提供一套图: 为了减少程序员的重复工作,一个APP

【转】android多分辨率适配

前一阶段开发android项目,由于客户要求进行多分辨率适配,能够支持国内主流的分辨率手机.因此经过了几次开发走了很多弯路,目前刚刚领略了android多分辨率适配的一些方法. 先介绍一下所走的弯路,由于android的布局文件存放在res的layout中,可以根据不同的手机分辨率指定特定的layou参数,如图所示:.根据不同的手机设定多个分辨率layout参数布局文件.因此再程序加载的过程中,会把运行该分辨率下的布局文件. 这样开发的问题是回到至布局文件很多,很乱.不方便管理.一旦修改需要修改

Android笔记:多分辨率适配及碎片化问题解决方案总结

一.适配多分辨率 1.官网介绍: http://developer.android.com/guide/practices/screens_support.html#qualifiers Screen characteristic Qualifier Description Size small Resources for small size screens. normal Resources for normal size screens. (This is the baseline siz

android多分辨率适配

前一阶段开发android项目,由于客户要求进行多分辨率适配,能够支持国内主流的分辨率手机.因此经过了几次开发走了很多弯路,目前刚刚领略了android多分辨率适配的一些方法. 先介绍一下所走的弯路,由于android的布局文件存放在res的layout中,可以根据不同的手机分辨率指定特定的layou参数,如图所示:.根据不同的手机设定多个分辨率layout参数布局文件.因此再程序加载的过程中,会把运行该分辨率下的布局文件. 这样开发的问题是回到至布局文件很多,很乱.不方便管理.一旦修改需要修改

(16)Cocos2d-x 多分辨率适配完全解析

Overview 从Cocos2d-x 2.0.4开始,Cocos2d-x提出了自己的多分辨率支持方案,废弃了之前的retina相关设置接口,提出了design resolution概念. 3.0中有以下相关接口: Director::getInstance()->getOpenGLView()->setDesignResolutionSize() //设计分辨率大小及模式 Director::getInstance()->setContentScaleFactor() //内容缩放因子

quick-cocos2d-x 多分辨率适配详解

多种分辨率的适配一直都是一个蛋疼的问题,各家公司可能都有自己的一套方案.今天我为大家介绍的是我们在多款游戏里实践后的解决方案,相对来说成本和实现难度都较低,效果也很不错. 多种分辨率适配的原理 因为横屏和竖屏的原理完全相同,所以本文先以竖屏为例,后文再说明横屏的处理. 制作一张 640×960 像素的图片,并传入设备查看: 查看时将图片缩放到合适大小,确保图片左右两边正好填满整个屏幕. 在不同分辨率的设备上,这张图片的显示效果差异体现在图片上下是否能够填满屏幕. 例如在 480×800 的设备上

Unity3d + NGUI 的多分辨率适配

移动端的多机型适配 现在要介绍的是<锁链战记>这款游戏的适配方法,这种适配方法是UI是一个基础尺寸,背景是一个基础尺寸,背景比UI多出的部分是一些没有实际作用的部分,这样的适配方式避免了在iPhone5这样的小屏幕上镶边. 首先设定UIRoot的Scaling Style属性,如果是电脑现在FixedSize,如果要打包到移动端选择FixedSizeOnMobiles. 我这里是以960*640为UI基础尺寸所以这里填写640高. 下面编写脚本BaseAspect.cs using Unity

Cocos与Cocos2d-x协作教程——多分辨率适配

http://www.cocoachina.com/bbs/read.php?tid-288123.html Cocos v2.1开始新增了一种新的多分辨率适配方案:流式布局. 这种布局相比CocosStudio v1.x时代简单纯粹得多,没那么多复杂的概念也没那么难理解,上手更简单,这篇教程就是来教大家怎么使用这个功能的. 实际上之前也写过一篇布局教程,不过反响感觉不太好,不少人看完之后还有不少疑问,尤其是跟2d-x协作相关的疑问.因此,这次提供一个更给力的版本,从Cocos编辑配置到与Coc

【Cocos2d-x 017】 多分辨率适配全然解析

转:http://blog.csdn.net/w18767104183/article/details/22668739 文件夹从Cocos2d-x 2.0.4開始,Cocos2d-x提出了自己的多分辨率支持方案.废弃了之前的retina相关设置接口,提出了design resolution概念. <ol class="linenums" style="margin: 0px; padding-left: 27px;"><li value=&quo