Android 多屏适配解决方案

来源:互联网 发布:淘宝宝贝主图大小 编辑:程序博客网 时间:2024/06/11 05:37


1.主流手机必要测量的参数(通过具体的方法,测量出,需要测试手机的 下面的这些参数,我们主要使用的只是 screenwidth  这个参数,其他参数只是帮助我们更好的理解

屏幕适配)


 DisplayMetrics metric = new DisplayMetrics();
        activity.getWindowManager().getDefaultDisplay().getMetrics(metric);
        int width = metric.widthPixels;  // 屏幕宽度(像素)
        int height = metric.heightPixels;  // 屏幕高度(像素)
        float density = metric.density;  // 屏幕密度(0.75 / 1.0 / 1.5/2.0)
        int densityDpi = metric.densityDpi;  // 屏幕密度DPI(120 / 160 / 240/320)
        int screenWidth = (int) (width / density);//屏幕宽度(dp)
        int screenHeight = (int) (height / density);//屏幕高度(dp)
        AppContext.getAppContext().setDpHeight(screenWidth);
        AppContext.getAppContext().setDpWidth(screenHeight)

        Log.i("pixel", "width:" + width + "height:" + height + " density:" + density + " densitydpi" + densityDpi);

具体手机型号,对应的输出参数

华为 ht:960 density:1.5densitydpi240  screenWidth360  screenHeight640

米2      +++width:720  height:1280 density:2.0 densitydpi320  screenWidth360  screenHeight640

HM1s    +++width:720  height:1280density:2.0 densitydpi320  screenWidth360  screenHeight640

MI 3   +++width:1080  height:1920density:3.0 densitydpi480 screenWidth360  screenHeight640

GT-I9507v  +++width:1080  height:1920density:3.0 densitydpi480 screenWidth360  screenHeight640

荣耀3c     +++width:720  height:1280 density:2.0 densitydpi320  screenWidth360  screenHeight640

华为 G730-U00  +++width:540  height:960 density:1.5 densitydpi240  screenWidth360  screenHeight640

华为  A199 +++width:720  height:1280density:2.0 densitydpi320  screenWidth360  screenHeight640

努比亚Nx507J    +++width:1080  height:1920 density:3.0 densitydpi480  screenWidth360  screenHeight640

联想K910  +++width:1080 height:1920 density:3.0 densitydpi480 screenWidth360  screenHeight640 

华为P6   +++width:720 height:1184 density:2.0 densitydpi320 screenWidth360  screenHeight592

Coolpad 8675   width:720 height:1280 density:2.0 densitydpi320  width 360    height  640

华为 G520    +++width:480  height:854 density:1.5 densitydpi240  screenWidth320  screenHeight569

HTC T528w  +++width:480  height:800density:1.5 densitydpi240 screenWidth320  screenHeight533

三星 N7000  width:800height:1280 density:2.0densitydpi320 width 400 dp  height 640 dp


2.根据google api 提供的

   适配命名优先级,国家,sw参数dp  等等 ,类似 values-1920x1080 这种书写方式,官方api上没有具体进行说明,但是经过测试的却可以使用这种方式进行适配,缺点就是

  需要定义很多类似这样的配置参数,

3.第三点,需要在项目values -dimens(基础为系统默认提供),写下具体的适配参数,由于考虑到现在收据的screenwidth 都是在320dpi之上,所以,我们项目中使用1dp=2px 作为基础dimens适配参数,这样

  

   我们就需要根据不同的手screenwidth建立不同的 sw文件夹


     sw320dp

     sw360dp(较多)

     sw400dp

    sw480dp(市面上较少)

  4.根据基础适配参数320dp,如果我们需要适配360dp则需要,使用(360/320)*基础dimens参数,一侧类推,如果是400dp适配,则使用(400/320)*基础dimens参数

 5.根据不同的适配需要,首先在基础dimens中进行配置,然后,测试各个手机适配情况进行相应的调整



   总结:之前适配,是从网上找的一段代码,经所有的适配条件,根据比值(0.75/1/1.5/2)事先计算好。然后需要适配的地方使用自己计算好的参数。这样适配肯定是不行,

     后期维护成本较高,适配效果不够精细。


  以上都是手动敲上去的,,因为之前做适配,没去读google api 造成走了很大的弯路。google api 多屏适配上面写的都是清清楚楚的,我们参照各市面上几个较有名的apk,都是这样实现的,京东的apk大家可以反编译看看,里面的适配使用了5种之多,(这是很笨的方式),淘宝,微信,Facebook,sina 客户端,均没有采用京东的做法。

     适配后的文件:


    


0 0