Reputation: 893
I want to accurately convert pixels to inches and mm.
I had tried to use all the properties of displayMetrics
but was unsuccessful in getting the exact physical units in mobile.
I also tried to use TypedValue.applyDimension
but as the documentation is not much explanatory still was unlucky.
This is the code I tried with displayMetrics
displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
when I try to get Pixels Per Inch by using
displayMetrics.ydpi
or displayMetrics.xdpi
, I am not getting the exact values as I am checking it by drawing a line of that much length on canvas and checked with actual ruler.
Upvotes: 3
Views: 3355
Reputation: 421
try this
DisplayMetrics mDisplayMetric = this.getApplicationContext().getResources().getDisplayMetrics();
float anyPixel = View.getMeasuredWidth(); //you can get Pixel from anywhere, it is a example from view
float widthInches = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_IN, size.x, mDisplayMetric);
Upvotes: 2