The main record for this section is to get the various parameters related to screen adaptation from your code:
The Java code is as follows
public class Screenutil {/** * NOTE: * Only activity can use Getwindowmanager, otherwise * context.getresources () should be used. Getdisplaymetrics () to get *//** * Get displaymetric Related parameters * @param context * @return */public static String Getmetricparams (Activity context) {DISPL Aymetrics dm = new Displaymetrics (); Context.getwindowmanager (). Getdefaultdisplay (). Getmetrics (DM); return "Density:" +dm.density+ ";d ensitydpi:" +dm.densitydpi+ "; Height:" +dm.heightpixels+ "; width:" +dm.widthpixels+ "; scaleddensity:" +dm.scaleddensity+ "; xdpi:" +dm.xdpi+ "; ydpi:" +DM.YDPI;} /** * Get screen size in pixels * @param context * @return */public static String getscreensizeininpixels (Activity context) {DISPLAYMETR ICS DM = new Displaymetrics (); Context.getwindowmanager (). Getdefaultdisplay (). Getmetrics (DM);d ouble heightininpixels = (double) dm.heightpixels;double widthininpixels = (double) Dm.widthpixels;return "High:" +heightininpixels+ "width:" + widthininpixels+ "unit (pixel)";} /** * Get screen size in inches * Calculate screen size should use exact density: xdpi ydpi to calculate * Use normalized density: densitydpi is wrong, it is fixed value, * 120 160 240 320 480, according to DPCompute pixels to use it * @param context * @return */public static String Getscreensizeininch (Activity context) {displaymetrics dm = new D Isplaymetrics (); Context.getwindowmanager (). Getdefaultdisplay (). Getmetrics (DM);d ouble heightininch = (double) Dm.heightpixels/(double) dm.ydpi;double widthininch = (double) dm.widthpixels/(double) dm.xdpi;double Scrrensizeininch = math.sqrt (heightininch*heightininch+ widthininch*widthininch); return "High:" +heightininch+ "width:" + widthininch+ "Size:" +scrrensizeininch+ "unit (Inch)";} /** * Get screen size in DP * @param context * @return */public static String GETSCREENSIZEININDP (Activity context) {Displaymetrics DM = new Displaymetrics (); Context.getwindowmanager (). Getdefaultdisplay (). Getmetrics (DM); float HEIGHTININDP = Px2dip ( (context) context, (float) dm.heightpixels); float WIDTHININDP = Px2dip ((context) context, (float) dm.widthpixels); Return "High:" +heightinindp+ "width:" +widthinindp+ "unit (DP)";} /** * DP to PX * @param context * @param dpvalue * @return */public static int dip2px (context context, float Dpvalue) {Final float scale = context.getresources (). Getdisplaymetrics (). Density;return (int) (dpvalue*scale+0.5f);} /** * px to DP * @param context * @param pxvalue * @return */public static int Px2dip (context context, float Pxvalue) {final F Loat scale = Context.getresources (). Getdisplaymetrics (). Density;return (int) (pxvalue/scale+0.5f);}}
Settings FOR XML:
<support-screens
Android:anydensity = "true"
Android:largescreens = "true"
Android:normalscreens = "true"
Android:resizeable = "true"
Android:smallscreens = "true
Android:xlargescreens = "true" >
Android:anydensity = "true" when the application is installed on a different density terminal,
The program will load the resources in xxhdpi, xhdpi, hdpi, mdpi,ldpi folder respectively.
Android Adaptation Program summary (ii)