Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I got the designs for a new app. All the dimensions are Android-ready and are given in DP - (Density-independent Pixels). How can I convert these values to Flutter's LP (Logical Pixels). I know that Window.devicePixelRatio gives me the number of device pixels for each logical pixel.

What's the exact difference between DP and LP? Are there any built-in methods for dp to lp conversion?

like image 742
kosiara - Bartosz Kosarzycki Avatar asked Mar 06 '19 22:03

kosiara - Bartosz Kosarzycki


2 Answers

According to https://api.flutter.dev/flutter/dart-ui/FlutterView/devicePixelRatio.html there are roughly 38 logical pixels per centimeter, or about 96 logical pixels per inch, of the physical display.

And according to https://developer.android.com/training/multiscreen/screendensities,One dp is a virtual pixel unit that's roughly equal to one pixel on a medium-density screen (160dpi; the "baseline" density).

So we can say:

160 dp == 1 inch == 96 lp

like image 87
Daniel Dai Avatar answered Oct 20 '22 10:10

Daniel Dai


According to the documentation (window.devicePixelRatio and Flutter for Android Developers), there is no difference between DP and 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.

like image 43
Hoppeduppeanut Avatar answered Oct 20 '22 09:10

Hoppeduppeanut