android平台的多分辨率适配一直是众程序猿的噩梦。网上相关介绍很多。但不可避免的,错误的介绍也很多。在总结前人经验,以及自己实践后。有了下面一点心得。希望对大家有用。
首先,列一下相关资料和参数:
px和dip换算关系
pixs =dips*(densityDpi/160)
dips=(pixs*160)/densityDpi
例如:
一个手机的分辨率px: 1280 * 720,屏幕密度:densityDpi: 320dpi
则dip: 640dp * 360dp
关于资源适配,大家可以去看看这个文档
http://developer.android.com/guide/topics/resources/providing-resources.html
Table 2. Configuration qualifier names.
Configuration | Qualifier Values | Description |
---|---|---|
MCC and MNC | Examples: etc. | The mobile country code (MCC), optionally followed by mobile network code (MNC) from the SIM card in the device. For example, is U.S. on any carrier, is U.S. on Verizon, and is France on Orange. If the device uses a radio connection (GSM phone), the MCC and MNC values come from the SIM card. You can also use the MCC alone (for example, to include country-specific legal resources in your application). If you need to specify based on the language only, then use the language and region qualifier instead (discussed next). If you decide to use the MCC and MNC qualifier, you should do so with care and test that it works as expected. Also see the configuration fields , and , which indicate the current mobile country code and mobile network code, respectively. |
Language and region | Examples: etc. | The language is defined by a two-letter ISO 639-1 language code, optionally followed by a two letter ISO 3166-1-alpha-2 region code (preceded by lowercase " "). The codes are not case-sensitive; the prefix is used to distinguish the region portion. You cannot specify a region alone. This can change during the life of your application if the user changes his or her language in the system settings. See Handling Runtime Changes for information about how this can affect your application during runtime. See Localization for a complete guide to localizing your application for other languages. Also see the configuration field, which indicates the current locale. |
Layout Direction | | The layout direction of your application. means "layout-direction-right-to-left". means "layout-direction-left-to-right" and is the default implicit value. This can apply to any resource such as layouts, drawables, or values. For example, if you want to provide some specific layout for the Arabic language and some generic layout for any other "right-to-left" language (like Persian or Hebrew) then you would have: Note: To enable right-to-left layout features for your app, you must set to and set to 17 or higher. Added in API level 17. |
smallestWidth | Examples: etc. | The fundamental size of a screen, as indicated by the shortest dimension of the available screen area. Specifically, the device's smallestWidth is the shortest of the screen's available height and width (you may also think of it as the "smallest possible width" for the screen). You can use this qualifier to ensure that, regardless of the screen's current orientation, your application's has at least dps of width available for it UI. For example, if your layout requires that its smallest dimension of screen area be at least 600 dp at all times, then you can use this qualifer to create the layout resources, . The system will use these resources only when the smallest dimension of available screen is at least 600dp, regardless of whether the 600dp side is the user-perceived height or width. The smallestWidth is a fixed screen size characteristic of the device; the device's smallestWidth does not change when the screen's orientation changes. The smallestWidth of a device takes into account screen decorations and system UI. For example, if the device has some persistent UI elements on the screen that account for space along the axis of the smallestWidth, the system declares the smallestWidth to be smaller than the actual screen size, because those are screen pixels not available for your UI. Thus, the value you use should be the actual smallest dimension required by your layout (usually, this value is the "smallest width" that your layout supports, regardless of the screen's current orientation). Some values you might use here for common screen sizes:
When your application provides multiple resource directories with different values for the smallestWidth qualifier, the system uses the one closest to (without exceeding) the device's smallestWidth. Added in API level 13. Also see the attribute, which declares the minimum smallestWidth with which your application is compatible, and the configuration field, which holds the device's smallestWidth value. For more information about designing for different screens and using this qualifier, see the Supporting Multiple Screens developer guide. |
Available width | Examples: etc. | Specifies a minimum available screen width, in units at which the resource should be used—defined by the value. This configuration value will change when the orientation changes between landscape and portrait to match the current actual width. When your application provides multiple resource directories with different values for this configuration, the system uses the one closest to (without exceeding) the device's current screen width. The value here takes into account screen decorations, so if the device has some persistent UI elements on the left or right edge of the display, it uses a value for the width that is smaller than the real screen size, accounting for these UI elements and reducing the application's available space. Added in API level 13. Also see the configuration field, which holds the current screen width. For more information about designing for different screens and using this qualifier, see the Supporting Multiple Screens developer guide. |
Available height | Examples: etc. | Specifies a minimum available screen height, in "dp" units at which the resource should be used—defined by the value. This configuration value will change when the orientation changes between landscape and portrait to match the current actual height. When your application provides multiple resource directories with different values for this configuration, the system uses the one closest to (without exceeding) the device's current screen height. The value here takes into account screen decorations, so if the device has some persistent UI elements on the top or bottom edge of the display, it uses a value for the height that is smaller than the real screen size, accounting for these UI elements and reducing the application's available space. Screen decorations that are not fixed (such as a phone status bar that can be hidden when full screen) are not accounted for here, nor are window decorations like the title bar or action bar, so applications must be prepared to deal with a somewhat smaller space than they specify. Added in API level 13. Also see the configuration field, which holds the current screen width. For more information about designing for different screens and using this qualifier, see the Supporting Multiple Screens developer guide. |
Screen size | |
Note: Using a size qualifier does not imply that the resources are only for screens of that size. If you do not provide alternative resources with qualifiers that better match the current device configuration, the system may use whichever resources are the best match. Caution: If all your resources use a size qualifier that is larger than the current screen, the system will not use them and your application will crash at runtime (for example, if all layout resources are tagged with the qualifier, but the device is a normal-size screen). Added in API level 4. See Supporting Multiple Screens for more information. Also see the configuration field, which indicates whether the screen is small, normal, or large. |
Screen aspect | |
Added in API level 4. This is based purely on the aspect ratio of the screen (a "long" screen is wider). This is not related to the screen orientation. Also see the configuration field, which indicates whether the screen is long. |
Screen orientation | |
This can change during the life of your application if the user rotates the screen. See Handling Runtime Changes for information about how this affects your application during runtime. Also see the configuration field, which indicates the current device orientation. |
UI mode | |
Added in API level 8, television added in API 13. For information about how your app can respond when the device is inserted into or removed from a dock, read Determining and Monitoring the Docking State and Type. This can change during the life of your application if the user places the device in a dock. You can enable or disable some of these modes using . SeeHandling Runtime Changes for information about how this affects your application during runtime. |
Night mode | |
Added in API level 8. This can change during the life of your application if night mode is left in auto mode (default), in which case the mode changes based on the time of day. You can enable or disable this mode using . See Handling Runtime Changes for information about how this affects your application during runtime. |
Screen pixel density (dpi) | |
There is a 3:4:6:8 scaling ratio between the four primary densities (ignoring the tvdpi density). So, a 9x9 bitmap in ldpi is 12x12 in mdpi, 18x18 in hdpi and 24x24 in xhdpi. If you decide that your image resources don't look good enough on a television or other certain devices and want to try tvdpi resources, the scaling factor is 1.33*mdpi. For example, a 100px x 100px image for mdpi screens should be 133px x 133px for tvdpi. Note: Using a density qualifier does not imply that the resources are only for screens of that density. If you do not provide alternative resources with qualifiers that better match the current device configuration, the system may use whichever resources are the best match. See Supporting Multiple Screens for more information about how to handle different screen densities and how Android might scale your bitmaps to fit the current density. |
Touchscreen type | |
configuration field, which indicates the type of touchscreen on the device. |
Keyboard availability | |
resources, but not resources, the system uses the resources regardless of whether a keyboard is visible, as long as the system has a software keyboard enabled. This can change during the life of your application if the user opens a hardware keyboard. See Handling Runtime Changes for information about how this affects your application during runtime. Also see the configuration fields and , which indicate the visibility of a hardware keyboard and and the visibility of any kind of keyboard (including software), respectively. |
Primary text input method | |
configuration field, which indicates the primary text input method available. |
Navigation key availability | |
This can change during the life of your application if the user reveals the navigation keys. See Handling Runtime Changes for information about how this affects your application during runtime. Also see the configuration field, which indicates whether navigation keys are hidden. |
Primary non-touch navigation method | |
configuration field, which indicates the type of navigation method available. |
Platform Version (API level) | Examples: etc. | The API level supported by the device. For example, for API level 1 (devices with Android 1.0 or higher) and for API level 4 (devices with Android 1.6 or higher). See the Android API levels document for more information about these values. Caution: Android 1.5 and 1.6 only match resources with this qualifier when it exactly matches the platform version. See the section below about Known Issuesfor more information. |
表格中,从上往下优先级依次下降
知道了以上原则,我们就可以写一个小程序来计算资源的最优适配
package com.bravoon.showlayoutsize;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.util.DisplayMetrics;
import android.widget.TextView;
public class MainActivity extends Activity {
private DisplayMetrics dm;
private int screenWidth;
private int screenHeight;
private float density;
private int densityDpi;
private int layoutsize;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.activity_main);
//1. deprecated
// screenWidth = getWindowManager().getDefaultDisplay().getWidth();
// screenHeight = getWindowManager().getDefaultDisplay().getHeight();
//2. getResources
// dm = new DisplayMetrics();
// dm = getResources().getDisplayMetrics();
// screenWidth = dm.widthPixels;
// screenHeight = dm.heightPixels;
//3. getWindowManager
dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
screenWidth = dm.widthPixels;
screenHeight = dm.heightPixels;
density = dm.density;
densityDpi = dm.densityDpi;
layoutsize = calculateSizeOfLayout();
TextView tv = new TextView(this);
tv.setText("screenWidth = " + screenWidth + "\n" + "screenHeight = "
+ screenHeight + "\n" + "densityDpi = " + densityDpi + "\n"
+ "LayoutSize = layout-sw" + layoutsize + "dp" + "\n");
setContentView(tv);
}
private int calculateSizeOfLayout() {
int ls = 0;
int sw = (screenWidth > screenHeight) ? screenHeight : screenWidth;
if (densityDpi <= 120) {
ls = (int) sw * 4 / 3;
} else if (densityDpi > 120 && densityDpi <= 160) {
ls = (int) sw;
} else if (densityDpi > 160 && densityDpi <= 240) {
ls = (int) (sw / 1.5);
} else if (densityDpi > 240) {
ls = (int) sw / 2;
}
return ls;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
配置文件里,要加上写:
<supports-screens
android:anyDensity="true"
android:largeScreens="true"
android:normalScreens="true"
android:resizeable="true"
android:smallScreens="true" />
代码自己写的。可能会有一些错误。忘指正
源码我会上传在我的资源里。如果需要,大家可以过去下载。