- /**
- * Standard quantized DPI for low-density screens.
- */
- public static final int DENSITY_LOW = 120;
- /**
- * Standard quantized DPI for medium-density screens.
- */
- public static final int DENSITY_MEDIUM = 160;
- /**
- * Standard quantized DPI for high-density screens.
- */
- public static final int DENSITY_HIGH = 240;
/**
* Standard quantized DPI for low-density screens.
*/
public static final int DENSITY_LOW = 120;
/**
* Standard quantized DPI for medium-density screens.
*/
public static final int DENSITY_MEDIUM = 160;
/**
* Standard quantized DPI for high-density screens.
*/
public static final int DENSITY_HIGH = 240;
同样在这个类最下面,找到系统获取Density的方法:
- private static int getDeviceDensity() {
- // qemu.sf.lcd_density can be used to override ro.sf.lcd_density
- // when running in the emulator, allowing for dynamic configurations.
- // The reason for this is that ro.sf.lcd_density is write-once and is
- // set by the init process when it parses build.prop before anything else.
- return SystemProperties.getInt("qemu.sf.lcd_density",
- SystemProperties.getInt("ro.sf.lcd_density", DENSITY_DEFAULT));
- }
private static int getDeviceDensity() {
// qemu.sf.lcd_density can be used to override ro.sf.lcd_density
// when running in the emulator, allowing for dynamic configurations.
// The reason for this is that ro.sf.lcd_density is write-once and is
// set by the init process when it parses build.prop before anything else.
return SystemProperties.getInt("qemu.sf.lcd_density",
SystemProperties.getInt("ro.sf.lcd_density", DENSITY_DEFAULT));
}
从这个方法中可以看到,系统会去读系统属性。先去读ro.sf.lcd_density,若存在,那么设置这个属性记录的density的值。否则,则取默认值也就是160。所以简单的测试方法就是改build.prop中的ro.sf.lcd_density,重启测试发现果然屏幕的分辨率发生了变化,但是这样直接修改build.prop的效果在重编译后就覆盖了;如果要在安卓CODE中添加,则在DisplayHardware.cpp (frameworks\base\services\surfaceflinger\displayhardware) 中查找ro.sf.lcd_density,直接将property值写死。或者修改\alps\mediatek\config\projectname\system.prop中ro.sf.lcd_density的值,该文件中还可以设置ro.sf.hwrotation属性。
曾经碰到这样一个问题,WVGA的7寸LCD的平板,图标和字体看起来很大很不舒服,此时它的默认density是240,也即一英寸240像素;我们可以人工将它的density改小,比如180,这样字体和图标都是按英寸计算长度的,更新后的图标和字体就显得小多了。关于平板模式手机模式切换,以及的density影响请参见参考原文5。
因为该值有人为改变的可能性,可以用以下办法获取系统真实的DPI。样例基础是:http://blog.csdn.net/zhandoushi1982/article/details/8498822,添加如下即可。
- DisplayMetrics dm = new DisplayMetrics();
- getWindowManager().getDefaultDisplay().getMetrics(dm);
- scale.setText("scale is"+dm.densityDpi);
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
scale.setText("scale is"+dm.densityDpi);
==================================================================================================================
设置安卓应用中的横屏或竖屏,使不能自动旋转,在相应AndroidManifest.xml中对Activity属性进行设置。如: <activity android:name="ScreenLoading" android:screenOrientation="portrait">限制竖屏, 或者在配置中加入android:screenOrientation=”landscape”限制横屏。
一般的应用可以满足横竖屏两种布局的需要,因为手机屏幕方向变化的时候系统会自动调用相应的布局文件,但如果一种布局文件无法满足两种屏幕显示的时候,可以在res目录下建立layout-port和layout-land两个目录,里面分别放置竖屏和横屏两种布局文件就行了。
参考原文:http://blog.csdn.net/muojie/article/details/6656572
参考原文:http://blog.sina.com.cn/s/blog_72fad6270100wh5y.html
参考原文:http://blog.csdn.net/linweig/article/details/5029843
参考原文:http://blog.csdn.net/ponderforever/article/details/7296734
参考原文:http://blog.csdn.net/xubin341719/article/details/7960521#comments