如何将 Android 的 DP 转换为 Flutter 的 LP? DP和LP有什么区别?

How to convert Android's DP to Flutter's LP? What is the difference between DP and LP?

我得到了一个新应用程序的设计。所有尺寸都已 Android-ready 并以 DP - (Density-independent Pixels) 给出。如何将这些值转换为 Flutter 的 LP逻辑像素)。 我知道 Window.devicePixelRatio 为我提供了每个逻辑像素的设备像素数。

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

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

Device pixels are also referred to as physical pixels. Logical pixels are also referred to as device-independent or resolution-independent pixels.

Flutter doesn’t have dps but there are logical pixels, which are basically the same as device-independent pixels. The so-called devicePixelRatio expresses the ratio of physical pixels in a single logical pixel.

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

double getHeight(double screenHeightofthedeviceYouAreDebuging,BuildContextcontext,double size)
{

     return (MediaQuery.of(context).size.height / screenHeight) * size;

} 

所以,如果您在屏幕中使用 5 进行调试,则屏幕的高度将为 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));
}

根据 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