【问题标题】:How to convert Android's DP to Flutter's LP? What is the difference between DP and LP?如何将 Android 的 DP 转换为 Flutter 的 LP? DP和LP有什么区别?
【发布时间】:2019-07-28 16:39:18
【问题描述】:

我得到了一个新应用的设计。所有尺寸均适用于 Android,并以 DP -(与密度无关的像素)给出。如何将这些值转换为 Flutter 的 LP逻辑像素)。 我知道Window.devicePixelRatio 为我提供了每个逻辑像素的设备像素数。

DP 和 LP 之间的确切区别是什么? dp 到 lp 转换是否有任何内置方法?

【问题讨论】:

    标签: mobile flutter pixel units-of-measurement


    【解决方案1】:

    根据文档(window.devicePixelRatioFlutter for Android Developers),DP 和 LP 没有区别。

    设备像素也称为物理像素。逻辑像素也称为独立于设备或独立于分辨率的像素。

    Flutter 没有dps 但有逻辑像素,与设备无关像素基本相同。所谓devicePixelRatio,表示的是单个逻辑像素中物理像素的比例。

    【讨论】:

      【解决方案2】:

      您可以使用下面的代码让您的手机屏幕响应:

      double getHeight(double screenHeightofthedeviceYouAreDebuging,BuildContextcontext,double size)
      {
      
           return (MediaQuery.of(context).size.height / screenHeight) * size;
      
      } 
      

      因此,如果您使用 5 in screen 进行调试,则屏幕高度将为 640 或 MediaQuery.of(context).size。 (宽度和高度)将为您提供测试设备的屏幕尺寸 screen Height of the device You Are Debuging = 640 context = BuildContext size = size you want to be as you image , container etc height 。 所以它会根据使用的设备转换屏幕的大小

      double getWidth(double screenWidthofthedeviceYouAreDebuging,BuildContext context,double size){
      
        return (MediaQuery.of(context).size.width / screenHeight) * size;
      
      }
      
      EdgeInsets padding(top,bottom,left,right,context){
          return EdgeInsets.only(
            top: getHeight(640, context, top),
            bottom: getHeight(640, context, bottom),
            left: getHeight(640, context, left),
            right: getHeight(640, context, right));
      }
      

      【讨论】:

        【解决方案3】:

        根据https://api.flutter.dev/flutter/dart-ui/FlutterView/devicePixelRatio.html,物理显示器每厘米大约有 38 个逻辑像素,或每英寸大约有 96 个逻辑像素。

        根据https://developer.android.com/training/multiscreen/screendensities,One,dp 是一个虚拟像素单位,大致等于中等密度屏幕(160dpi;“基线”密度)上的一个像素。

        所以我们可以说:

        160 dp == 1 英寸 == 96 lp

        【讨论】:

          猜你喜欢
          • 2013-05-18
          • 2011-01-02
          • 2016-12-20
          • 2018-01-13
          • 2017-07-19
          • 1970-01-01
          • 2012-11-16
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多