现在Android智能手机在市场上非常普遍,各式各样的款式,各种类型,各种屏幕大小不一,真的挺为用户感到高兴,但有部分人还是挺郁闷的,那就是程序猿,为什么这样说呢?现在的android程序猿工作量非常大,在哪方面体现呢?那就是对手机进行适配。什么是适配,那就是要将某个应用在不同屏幕上显示效果要一致或者是不能乱。

上面的是作为android开发工程师的感受,现进入正题:

要做到适配,那先了解主流的手机是哪些分辨率,比如480*800,720*1280等,那怎么样获取这些信息?下面是获取信息的代码:

[html]  view plain  copy  
  1.         DisplayMetrics dm = new DisplayMetrics();    
  2.         getWindowManager().getDefaultDisplay().getMetrics(dm);    
  3.     
  4. float density  = dm.density;      // 屏幕密度(像素比例:0.75/1.0/1.5/2.0)    
  5. int densityDPI = dm.densityDpi;     // 屏幕密度(每寸像素:120/160/240/320)    
  6. float xdpi = dm.xdpi;             
  7. float ydpi = dm.ydpi;    
  8.     
  9. Log.e("  Jeny", "xdpi=" + xdpi + "ydpi=" + ydpi);    
  10. Log.e( "  Jeny", "density=" + density + "densityDPI=" + densityDPI);    
  11.     
  12. int screenWidthDip = dm.widthPixels;        // 屏幕宽(dip,如:320dip)    
  13. int screenHeightDip = dm.heightPixels;      // 屏幕宽(dip,如:533dip)    
  14.     
  15. Log.e("  Jeny", "screenWidthDip=" + screenWidthDip + "screenHeightDip=" + screenHeightDip);    
  16.     
  17. int screenWidth  = (int)(dm.widthPixels * density + 0.5f);      // 屏幕宽(px,如:480px)    
  18. int screenHeight = (int)(dm.heightPixels * density + 0.5f);     // 屏幕高(px,如:800px)    
  19.     
  20. Log.e("  Jeny", "screenWidth=" + screenWidth + "screenHeight=" + screenHeight);         
  21.   
  22. Configuration config = getResources().getConfiguration();  
  23.   
  24.       int  smallestScreenWidth = config.smallestScreenWidthDp;      // 屏幕最小宽  
  25.         
  26.       Log.e("  Jeny", "smallestScreenWidth=" + smallestScreenWidth);  

以上代码可以让你了解你用的手机上的信息,但最主要的还是int  smallestScreenWidth = config.smallestScreenWidthDp; 这句,获取屏幕最小宽,因为以下适配方案是需要根据该值来进行解说的:

在获取smallestScreenWidth值后,在资源文件中新增values-sw300dp、values-sw400dp类似的文件夹,values-sw300dp表示最小宽为300dp的手机会使用该文件夹下的资源;values-sw400dp表示最小宽为400dp的手机会使用该文件夹下的资源。这个是个人测试获取的结果,分享出来,希望能帮助android工程师们!

更多相关文章

  1. Android屏幕的刷新机制
  2. 【android】适配多屏幕的最佳实践
  3. Android 手机屏幕那些事儿
  4. Android,关于设备独立像素(dip/dp),关于机型屏幕适配
  5. Android 屏幕尺寸和分辨率的适配
  6. Android实现系统级屏幕录制(上)

随机推荐

  1. mysql查询的时候给字段赋默认值操作
  2. mysql 主从复制如何跳过报错
  3. MySql分组后随机获取每组一条数据的操作
  4. 获取 MySQL innodb B+tree 的高度的方法
  5. MySQL之范式的使用详解
  6. linux使用mysqldump+expect+crontab实现m
  7. SQL 列不同的表查询结果合并操作
  8. MySQL5.7.31 64位免安装版使用教程图解
  9. 在SQL中对同一个字段不同值,进行数据统计
  10. 在sql中对两列数据进行运算作为新的列操