Android adaptation solution Summary (II)

Source: Internet
Author: User

Android adaptation solution Summary (II)

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:

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.




Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.