Displaymetrics Class-A Structured description of a display's general information, including its size, density, and character scaling ratio.
Public float density; // screen pixel density value. The density value indicates the number of display points per inch, which is different from the resolution.
Android has the following screens:
Qvga and wqvga screens density = 120;
Hvga screen density = 160;
WVGA screen density = 240;
The following uses 240 dip * 800dip WVGA (density =) as an example to list the screen resolution information of different density:
When density = 120, the actual screen resolution is 240px * 400px (two points correspond to one resolution)
The height of the status bar is 19px or 25dip.
The screen width is 400px or 800dip, and the working area height is 211px or 480dip.
Screen width: Px or dip, working area Height: 381px or 775dip
When density = 160, the actual screen resolution is 320px * 533px (three points correspond to two resolutions)
The height of the status bar is 25px or 25dip.
The screen width is 533px or 800dip, and the working area height is 295px or 480dip.
Screen width: 320px or 480dip, working area Height: 508px or 775dip
When density = 240, the actual screen resolution is 480px * 800px (one point for one resolution)
The height of the status bar and title bar is 38px or 25dip.
The screen width is PX or dip, and the working area height is PX or dip.
Screen width 480px or 480dip, working area height 762px or 775dip
In the APK resource package, when the screen density is 240, the resources using the hdpi label
Resources that use the mdpi label when the screen density is 160
Ldpi tag resources are used when the screen density is 120.
Resources without any tags are shared in various resolutions.
Suggestion: Use the unit dip as much as possible during layout, and use less PX.
Device Independent pixels (device independent pixels). Different devices have different display effects. This is related to the hardware of the device. We recommend that you use this feature to support WVGA, hvga, and qvga, without relying on pixels.
Import android. content. context; import android. util. displaymetrics; // Formula for Calculating pixels = dips * (density/160) public class densityutil {Private Static final string tag = densityutil. class. getsimplename (); // The densitydpi Private Static float dmdensitydpi = 0.0f; Private Static displaymetrics DM; Private Static float scale = 0.0f; Public densityutil (context) of the current screen) {// get the current screen dm = new displaymetrics (); // Return the dispatchmetrics information of the current resource object. Dm = context. getapplicationcontext (). getresources (). getdisplaymetrics (); // sets densitydpi setdmdensitydpi (DM. densitydpi); // Density Factor scale = getdmdensitydpi ()/160; // scale = DM. density; logger. I (TAG, tostring ();} public static float getdmdensitydpi () {return dmdensitydpi;} public static void setdmdensitydpi (float dmdensitydpi) {densityutil. dmdensitydpi = dmdensitydpi;} public static int dip2px (float dipvalue) {return (INT) (dipvalue * scale + 0.5f);} public int px2dip (float pxvalue) {return (INT) (pxvalue/scale + 0.5f) ;}@ override Public String tostring () {return "dmdensitydpi:" + dmdensitydpi ;}} others: // dip to pixel public static int diptopixels (context, int DIP) {final float scale = context. getresources (). getdisplaymetrics (). density; float valuedips = dip; int valuepixels = (INT) (valuedips * scale + 0.5f); Return valuepixels;} // convert the pixel to dip public static float pixelstodip (context, int pixels) {final float scale = context. getresources (). getdisplaymetrics (). density; float dips = pixels/scale; return dips;} // specify the image length and width to generate a public static bitmap decodebitmap (Bitmap initialbitmap, int height, int weight) {int bmpheight = initialbitmap. getheight (); int BMP Weight = initialbitmap. getwidth (); float scale = math. min (height/bmpheight, weight/BMP weight); bitmap mutablebitmap = bitmap. createscaledbitmap (initialbitmap, (INT) (BMP weight * scale), (INT) (bmpheight * scale), true); // specify the image length and width to generate a new image return mutablebitmap ;} // save bitmap as the specified JPG file public static void writephotojpg (bitmap data, string pathname) {file = new file (pathname); try {file. createnewfile (); // bufferedoutputstream OS = new bufferedoutputstream (// new fileoutputstream (File); fileoutputstream OS = new fileoutputstream (File); data. compress (bitmap. compressformat. JPEG, 100, OS); OS. flush (); OS. close (); mydebug. I ("writephotojpg");} catch (exception e) {e. printstacktrace () ;}}// save bitmap as the specified PNG file public static void writephotopng (bitmap data, string pathname) {file = new file (pathname); try {file. createnewfile (); fileoutputstream OS = new fileoutputstream (File); // bufferedoutputstream OS = new bufferedoutputstream (// new fileoutputstream (File); data. compress (bitmap. compressformat. PNG, 100, OS); OS. flush (); OS. close (); mydebug. I ("writephotopng");} catch (exception e) {e. printstacktrace ();}}