zoukankan      html  css  js  c++  java
  • Android 多屏适配解决方式


    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 + "height:" + height + " density:" + density + " densitydpi" + densityDpi);

    详细手机型号,相应的输出參数

    华为 ht:960 density:1.5densitydpi240  screenWidth360  screenHeight640

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

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

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

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

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

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

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

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

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

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

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

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

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

    三星 N7000  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 client。均没有採用京东的做法。

         适配后的文件:


        


  • 相关阅读:
    安卓linux真机调试
    Java开发必装的IntelliJ IDEA插件
    在switch中的case语句中声明变量会被提前
    PostgresSQL 数组包含@>
    SQL 增加列、修改列、删除列
    ConcurrentHashMap 的实现原理
    Apache——DBUtils框架ResultSetHandler接口使用
    [转](不理想)Ubuntu下更改主显示器
    [问题记录]Java关于可变参数重载问题的测试
    使用openssl生成双向加密证书(转)
  • 原文地址:https://www.cnblogs.com/gccbuaa/p/7375907.html
Copyright © 2011-2022 走看看