Multiply Screen Support(多屏幕适配)


Screen size
Actual physical size, measured as the screen's diagonal.

For simplicity, Android groups all actual screen sizes into four generalized sizes: small, normal, large, and extra-large.

Screen density
The quantity of pixels within a physical area of the screen; usually referred to as dpi (dots per inch). For example, a "low" density screen has fewer pixels within a given physical area, compared to a "normal" or "high" density screen.

For simplicity, Android groups all actual screen densities into six generalized densities: low, medium, high, extra-high, extra-extra-high, and extra-extra-extra-high.

Orientation
The orientation of the screen from the user's point of view. This is either landscape or portrait, meaning that the screen's aspect ratio is either wide or tall, respectively. Be aware that not only do different devices operate in different orientations by default, but the orientation can change at runtime when the user rotates the device.
Resolution
The total number of physical pixels on a screen. When adding support for multiple screens, applications do not work directly with resolution; applications should be concerned only with screen size and density, as specified by the generalized size and density groups.
Density-independent pixel (dp)
A virtual pixel unit that you should use when defining UI layout, to express layout dimensions or position in a density-independent way.
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.

A set of six generalized densities:
  • ldpi (low) ~120dpi
  • mdpi (medium) ~160dpi
  • hdpi (high) ~240dpi
  • xhdpi (extra-high) ~320dpi
  • xxhdpi (extra-extra-high) ~480dpi
  • xxxhdpi (extra-extra-extra-high) ~640dpi
 
  • 36x36 (0.75x) for low-density
  • 48x48 (1.0x baseline) for medium-density
  • 72x72 (1.5x) for high-density
  • 96x96 (2.0x) for extra-high-density
  • 144x144 (3.0x) for extra-extra-high-density
  • 192x192 (4.0x) for extra-extra-extra-high-density (launcher icon only; see note above)


Table 1. Configuration qualifiers that allow you to provide special resources for different screen configurations. 

Screen characteristic Qualifier Description
Size small Resources for small size screens.
normal Resources for normal size screens. (This is the baseline size.)
large Resources for large size screens.
xlarge Resources for extra-large size screens.
Density ldpi Resources for low-density (ldpi) screens (~120dpi).
mdpi Resources for medium-density (mdpi) screens (~160dpi). (This is the baseline density.)
hdpi Resources for high-density (hdpi) screens (~240dpi).
xhdpi Resources for extra-high-density (xhdpi) screens (~320dpi).
xxhdpi Resources for extra-extra-high-density (xxhdpi) screens (~480dpi).
xxxhdpi Resources for extra-extra-extra-high-density (xxxhdpi) uses (~640dpi). Use this for the launcher icon only, see noteabove.
Orientation land Resources for screens in the landscape orientation (wide aspect ratio).
port Resources for screens in the portrait orientation (tall aspect ratio).

Note: If you're developing your application for Android 3.2 and higher, see the section about Declaring Tablet Layouts for Android 3.2 for information about new configuration qualifiers that you should use when declaring layout resources for specific screen sizes (instead of using the size qualifiers in table 1).



Table 2. New configuration qualifiers for screen size (introduced in Android 3.2).  

sw<N>dp : sw表示SmallestWidth, N表示 W(屏幕宽度)>N时
w<N>dp : w表示Availiable Screen Width, W(屏幕宽度)=N
h<N>dp : h表示Availiable Screen Height, H(屏幕高度)=N

比如Table3, 1280X800, sw=800, w=1280, h=800


Table 3. Various screen configurations available from emulator skins in the Android SDK (indicated in bold) and other representative resolutions.

  Low density (120), ldpi Medium density (160), mdpi High density (240), hdpi Extra-high-density (320), xhdpi
Small screen QVGA (240x320)   480x640  
Normal screen WQVGA400 (240x400) 
WQVGA432 (240x432)
HVGA (320x480) WVGA800 (480x800) 
WVGA854 (480x854)
 
600x1024
640x960
Large screen WVGA800** (480x800) 
WVGA854** (480x854)
WVGA800* (480x800) 
WVGA854* (480x854)
 
600x1024
   
Extra-Large screen 1024x600 WXGA (1280x800)
1024x768
1280x768
1536x1152
1920x1152 
1920x1200
2048x1536
2560x1536 
2560x1600
* To emulate this configuration, specify a custom density of 160 when creating an AVD that uses a WVGA800 or WVGA854 skin.
** To emulate this configuration, specify a custom density of 120 when creating an AVD that uses a WVGA800 or WVGA854 skin.
† This skin is available with the Android 3.0 platform

dip或dp (device independent pixels)设备独立像素, 与设备屏幕有关。
dpi(dot per inch):屏幕像素密度,每英寸多少像素
density:density表示每英寸有多少个显示点(逻辑值),它的单位是dpi
像素密度和分辨率是两个不同的概念,分辨率是总的像素点,像素密度是单位长度的像素点

values - sw720dp          10.1  tablet   1280x800  mdpi

values-sw600dp          7.0   tablet   1024x600  mdpi

values-sw480dp          5.4           480x854   mdpi 
values - sw480dp          5.1           480x800   mdpi 

values-xhdpi            4.7           1280x720  xhdpi 
values - xhdpi            4.65 ”           720x1280  xhdpi 

values-hdpi              4.0            480x800  hdpi
values - hdpi              3.7            480x854  hdpi

values-mdpi              3.2            320x480  mdpi

values-ldpi              3.4            240x432  ldpi
values - ldpi              3.3            240x400  ldpi
values-ldpi             2.7           240x320 ldpi

可以使用下面的代码来测试sw为多少

Configuration config = getResources().getConfiguration();
int smallestScreenWidth = config.smallestScreenWidthDp;
Log.d("jack", "smallest width="+smallestScreenWidth);



HUAWEI C8816D 5英寸960x540像素 360
Nexus 77英寸800*1280像素800
Google Nexus 1010英寸2560x1600像素800

layout  this is the default, it is needed even if you don't plan to support phones!
layout-large  for 7" tablet (works on emulator and nexus7)
layout-xlarge  for 10" tablet (works on emulator and galaxytab10.1)


在平板的开发中有一个问题, 在清单文件中设置横竖屏转动不会重启Activity, Activity分为俩块Fragment, 横屏时左右比为1:2, 而竖屏需要比例为1:1。

不重启Activity,那么不会再去思考重绘页面,那样太难受了,不但数据要保留和再显示,还有一些控件的状态也要保留,比如spinner。
所以回到页面的宽度上面来。
在清单文件的Activity的属性设置android:configChanges="orientation|keyboardHidden|screenSize"
加入后横竖屏切换不会调用onCreate() 方法。
然后在Activity的布局文件中设置左右俩边的宽度
左边设死,右边权重为1填满即可。。。。 关键是左边要设置好,width = sw/2, 这样在平板上一般来说保证了横屏为1:2, 竖屏为1:1

<?xml version="1.0" encoding="utf-8"?>
     <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
         android:layout_width="fill_parent"
         android:layout_height="fill_parent">

          <include layout="@layout/toolbar"/>

         <!--  因为在sm=800的情况下,那么左边只需要设置具体宽度为400,那么在横屏会有1:2的效果, 在竖屏下1:1的效果  -->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_below="@id/toolbar"
        android:layout_height="match_parent">

        <FrameLayout
            android:id="@+id/layLeft"
            android:layout_width="@dimen/_400dp"
            android:layout_height="match_parent"/>

        <FrameLayout
            android:id="@+id/layRight"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent"/>

         </LinearLayout>
</RelativeLayout>


在适配的时候采用sw=800作为标准开发,命名规则为_XXdp, 比如在sw=800中需要使用10dp, 那么在values-sw800dp下增加一个<dimen name="_10dp">10dp</dimen>, 当完成开发后再写个脚本统一进行适配,比如在sw=600中,换算比例为600/800 = 3/4 , 那么在values-sw600dp下应该有个<dimen name="_10dp">7.5dp</dimen>, 同理在sw=360, sw=480, sw=720 或者更高的sw=1280 等....  当然字体适配没有那么大的变化,一般来说只有那么几个常用的. 





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值