问题
How do I calculate PPI of Android device. Most specifically Android Tablets. Take a Note that I want to calculate PPI of the device and not DPI.
回答1:
It's easy like one-two-three =) Let's caculate PPI to Nexus 5, for example.
float LCD_Diagonal = 4.95f;
int screenHeightPx = 1920;
int screenWidthPx = 1080;
float PPI = (float)Math.sqrt((double)(screenWidthPx*screenWidthPx + screenHeightPx*screenHeightPx))/LCD_Diagonal;
Log.e(TAG, "display_page_screen_ppi: " + String.format(Locale.getDefault(),"%d %s", Math.round(PPI), "ppi"));
In result we have 445 ppi
that's true according with specs.
回答2:
PPI
The screen density is quoted as Pixels Per Inch, PPI, and is the number of pixels that fit into an inch. The higher the number then the sharper images look on the display, therefore consumers consider a high PPI figure an advantage when buying a device. Sometimes the figure is quoted as Dots Per Inch, DPI, though the use of DPI is confusing as it comes from printing terminology, and it can refer to a the sensitivity of the sensors in digital scanners and digital cameras. For Android screens when it comes to PPI vs. DPI use PPI and leave DPI to refer to printers, where it is really a different measurement (because the dots needed to produce high quality images on paper are of greater density).
The PPI figure for a screen can be calculated given the resolution (number of x and y pixels) and size (diagonal measurement of the visible area) of a screen. Use the Pythagorean theorem to calculate the number of pixels in the diagonal then divide the result by the screen size. i.e. PPI=(square root of x^2 + y^2)/screen size
Second Option
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
// Display device width and height in pixels
screenHeightpx = dm.heightPixels;
screenWidthpx = dm.widthPixels;
回答3:
For that you need to take the diaplaymatrix.
DisplayMetrics dm = getResources().getDisplayMetrics();
int height = dm.heightPixels;
int width = dm.widthPixels;
来源:https://stackoverflow.com/questions/23890050/calculate-ppi-of-android-device