Pixel density in android

If I have an Android device with a screen size of 5 inches and a screen width of 2.3 inches, and its screen supports 1080 * 1920 pixels, what is the closest type of its screen density? How to resolve this issue?

+3


source to share


2 answers


You should use sp

for font sizes and dp

for anything else. See this SO post regarding screen density, terminology and correct usage: Difference between px, dp, dip and sp in Android?



Also, here is a link to the Android developer API guide for supporting multiple screens in your apps: http://developer.android.com/guide/practices/screens_support.html

+2


source


You can get information about device readings DisplayMetrics from Activity

DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);

int dpi = dm.densityDpi;

      

dm.densityDpi

will provide you with a pad for entry points expressed in dots per inch

You can also get other display metrics like:



float density       - The logical density of the display - scaling factor
float scaledDensity - A scaling factor for fonts displayed on the display.
int widthPixels     - The absolute width of the display in pixels.
int heightPixels    - The absolute height of the display in pixels.
float xdpi          - Physical pixels per inch of the screen in the X dimension.
float ydpi          - Physical pixels per inch of the screen in the Y dimension.

      

However, for some devices xdpi

, ydpi

metrics also return incorrect numbers and you cannot rely on them. Since API 17, you can also use getRealMetrics()

. It should give you the exact values โ€‹โ€‹for xdpi

and ydpi

.

DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getRealMetrics(dm);

      

+1


source







All Articles