Android Screen Resolution Tool class use detailed _android

Source: Internet
Author: User

Android Development We often need to use the dip, PX mutual conversion, get the phone screen width, height and status bar height, the following is based on the screen this piece of the collation of a class.

Package com.per.loadingwebviewdome;
Import Android.content.Context;

Import Android.util.DisplayMetrics;

Import Java.lang.reflect.Field; /** * @author: Xiaolijuan * @description: Screen resolution Tool class * @date: 2016-06-05 * @time: 22:55 * * public class Screenutil {/ * * * According to the mobile resolution of the DP into PX unit */public static int dip2px (context Mcontext, float dpvalue) {final float scale = Mcontext.getr
 Esources (). Getdisplaymetrics (). density;
 return (int) (Dpvalue * scale + 0.5f);  /** * According to the resolution of the mobile phone from px (pixel) units to become DP */public static int Px2dip (context Mcontext, float pxvalue) {final float scale
 = Mcontext.getresources (). Getdisplaymetrics (). density;
 return (int) (Pxvalue/scale + 0.5f); /** * Screen Width high * * @param mcontext context * @return/private static int[] getscreensize (contexts Mcontext) {Displaym
 Etrics dm = mcontext. Getresources (). Getdisplaymetrics ();
 int screenwidth = Dm.widthpixels;

 int screenheight = Dm.heightpixels;
 return new int[]{screenwidth, screenheight};
}

 /** * Get status bar Height * * @param mcontext context * @return/public static int getstatusbarheight (contextual mcontext) {class<?& Gt
 c = null;
 Object obj = null;
 Field field = NULL;
 int x = 0, statusbarheight = 0;
  try {c = class.forname ("Com.android.internal.r$dimen");
  obj = C.newinstance ();
  field = C.getfield ("Status_bar_height");
  x = Integer.parseint (Field.get (obj). toString ());
 Statusbarheight = Mcontext.getresources (). getdimensionpixelsize (x);
 catch (Exception E1) {e1.printstacktrace ();
 return statusbarheight; /** * Get mobile screen width * * @param mcontext context * @return/public static int getscreenwidth (contextual mcontext) {int S
 Creen[] = getscreensize (Mcontext);
 return screen[0]; /** * Get mobile screen Height * * @param mcontext context * @return/public static int getscreenheight (contextual mcontext) {int
 Screen[] = getscreensize (Mcontext);
 return screen[1];
 }
}

For dip, DP, PX and SP, before understand is the difference between dip and PX, we know that Google's recommendation, Pixel Unified use dip, Font Unified Use SP, then specify:

1. Dip:device independent pixels (device independent pixel): Different device display effect, dip and screen density, and screen density and equipment hardware, generally we in order to support WVGA, HVGA and QVGA recommended use this, do not rely on pixels. Dip Conversion: dip (value) = (int) (PX (value)/1.5 + 0.5)
2. DP: And dip is exactly the same, but the name is different.
3. Px:pixels (pixel), absolute pixel, different device different display effect is the same, this is how much will always be how much will not change.
4. sp:scaled pixels (magnified pixel). Mainly used for font display.

Here's a reference to the screen tool class above

LOG.E ("Get status bar height", string.valueof (Screenutil.getstatusbarheight (Mcontext)));
 LOG.E ("Get the width of the cell phone screen", String.valueof (Screenutil.getscreenwidth (Mcontext));
 LOG.E ("Get the height of the cell phone screen", String.valueof (Screenutil.getscreenheight (Mcontext));

The following is a printed log

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.