0

I have a pixel input and i want to convert it to DP using Java.

what formula should i use? or there is an external library than i can use!?

This code is common in android sdk. Is there a way to use it outside of Android?

public static float convertPixelsToDp(float px){
        DisplayMetrics metrics = Resources.getSystem().getDisplayMetrics();
        float dp = px / (metrics.densityDpi / 160f);
        return Math.round(dp);
    }

Iam not using it for android i use it in a java application so i want to know how to do it in java not android.

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Hatem
  • 11
  • 5
  • @TimBiegeleisen Iam not using it for android i use it in a java application so i want to know how to do it in java not android – Hatem Nov 25 '18 at 02:09
  • You should edit your question and make this clear then. I don't see any reason why the duplicate questions would not cover your use case though. – Tim Biegeleisen Nov 25 '18 at 02:20
  • IntelliJ is just an editor. What actual environment are you trying to write code for? – bmargulies Nov 25 '18 at 02:34
  • @bmargulies java application – Hatem Nov 25 '18 at 02:53
  • @Hatem what exactly are you using it for in your application? Is this for an image or for a screen? – vs97 Nov 25 '18 at 03:00
  • @vshcherbinin i have an input file that have some pixel values and i want to convert them to dp values and add this values into a file all just i need is the conversion formula or method – Hatem Nov 25 '18 at 03:05
  • 1
    @Hatem well, the formula for pixel-to-dp is dp = px / (dpi / 160). You can just use that straight away, if you know what your dpi value is. The dpi value is a smartphone related concept, so I dont know why you want to do a desktop application out of it. – vs97 Nov 25 '18 at 03:10
  • @vshcherbinin my application generates xml files that can be used for android development :) – Hatem Nov 25 '18 at 03:14
  • https://developer.android.com/training/multiscreen/screendensities#dips-pels – OneCricketeer Nov 25 '18 at 03:16

0 Answers0