Android adaptation solution Summary (II): android adaptation solution Summary
This section describes how to obtain parameters related to screen adaptation from the code:
The Java code is as follows:
Public class ScreenUtil {/*** Note: * only the activity can use getWindowManager. Otherwise, use * Context. getResources (). getDisplayMetrics () to get * // *** get DisplayMetric parameters * @ param context * @ return */public static String getMetricParams (Activity context) {DisplayMetrics dm = new DisplayMetrics (); context. getWindowManager (). getdefadisplay display (). getMetrics (dm); return "density:" + dm. density + "; densityDpi:" + dm. densityDpi + "; height:" + dm. heightPixels + "; width:" + dm. widthPixels + "; scaledDensity:" + dm. scaledDensity + "; xdpi:" + dm. xdpi + "; ydpi:" + dm. ydpi;}/*** get the screen size in pixels * @ param context * @ return */public static String getscreensizeinpixels (Activity context) {DisplayMetrics dm = new DisplayMetrics (); context. getWindowManager (). getdefadisplay display (). getMetrics (dm); double heightInInPixels = (double) dm. heightPixels; double widthInInPixels = (double) dm. widthPixels; return "height:" + heightinpixels + "width:" + widthInInPixels + "unit (pixel)";}/*** get screen size, the Unit is inches * the screen size should be calculated using the precision density: xdpi ydpi * The normalization density: densitydpi is incorrect, it is a fixed value, * 120 160 240 320, it is used to calculate pixels Based on dp * @ param context * @ return */public static String getscreensizeinch (Activity context) {DisplayMetrics dm = new DisplayMetrics (); context. getWindowManager (). getdefadisplay display (). getMetrics (dm); double heightinch = (double) dm. heightPixels/(double) dm. ydpi; double widthInInch = (double) dm. widthPixels/(double) dm. xdpi; double scrrensizeinch = Math. sqrt (heightinch * heightinch + widthinch * widthinininch); return "height:" + heightInInch + "width:" + widthInInch + "Size:" + ceiling + "unit (INCHES) ";}/*** get the screen size, in the unit of dp * @ param context * @ return */public static String getscreensizeindp (Activity context) {DisplayMetrics dm = new DisplayMetrics (); context. getWindowManager (). getdefadisplay display (). getMetrics (dm); float heightindp = px2dip (Context) context, (float) dm. heightPixels); float widthInInDp = px2dip (Context) context, (float) dm. widthPixels); return "height:" + heightindp + "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 );} /*** convert px to dp * @ param context * @ param pxValue * @ return */public static int px2dip (Context context, float pxValue) {final float scale = context. getResources (). getDisplayMetrics (). density; return (int) (pxValue/scale + 0.5f );}}
XML settings:
<Support-screens
Android: anyDensity = "true"
Android: largeScreens = "true"
Android: normalScreens = "true"
Android: resizeable = "true"
Android: smallScreens = "true
Android: xlargeScreens = "true">
Android: anyDensity = "true", applications are installed on terminals of different density,
The program loads Resources in the xxhdpi, xhdpi, hdpi, mdpi, and ldpi folders respectively.