Android Share notes (1) Get screen size, including status bar

Source: Internet
Author: User

A large wave of dry goods, screen size of all kinds of access, status bar size can be correctly obtained, pro-test.

package com.elyar.app.util.measure;import java.lang.reflect.field;import android.app.activity; import android.util.displaymetrics;public class displaymeasure { /**  *  note: Personal experience not to argue <br>  *  only activity can use Getwindowmanager, otherwise you should use <br>  *  context.getresources (). Getdisplaymetrics () to get .<br>  *   *  @author  ElyarAnwar 2016-1-29 21:24 Aksu  *   */ public  Displaymeasure ()  {  }   /**  *  get screen size in pixels   * @ Param context activity context   *  @return   Screen width   units px  */ public  static int getscreenwidthpixel (Activity context) {  displaymetrics  Outmetrics = new displaymetrics ();   context.getwindowmanager (). GetDefaultDisplay (). Getmetrics (outmetrics);   int widthpixels = outmetrics.widthpixels;  return widthpixels; }  /**   *  get screen size in pixels   *  @param  context activity Context   * @ return  screen height   unit Px  */ public static int getscreenheightpixel (Activity  context) {  displaymetrics outmetrics = new displaymetrics ();   Context.getwindowmanager (). Getdefaultdisplay (). Getmetrics (outmetrics);   int heightpixels =  outMetrics.heightPixels;  return heightPixels; }  /**  *  Get screen size in dp  *  @param  context activity context   *  @return   screen width   Unit DP&NBSP;&NBSP;*/&NBSP;PUBLIC&NBSP;STATIC&NBSP;INT&NBSP;GETSCREENWIDTHINDP (Activity context) {   displaymetrics outmetrics = new displaymetrics ();   Context.getwindowmanager (). Getdefaultdisplay (). GETmetrics (outmetrics);  int widthdp = outmetrics.widthpixels;  return  CHANGEUNITS.PX2DP (CONTEXT,&NBSP;WIDTHDP);//convert px to dp }  /**  *  to get screen size in dp   *  @param  context activity Context   *  @return   screen height   unit dp  */ &NBSP;PUBLIC&NBSP;STATIC&NBSP;INT&NBSP;GETSCREENHEIGHTINDP (activity context) {  DisplayMetrics  outmetrics = new displaymetrics ();   context.getwindowmanager (). Getdefaultdisplay (). Getmetrics (Outmetrics);  int heightdp = outmetrics.heightpixels; &NBSP;&NBSP;RETURN&NBSP;CHANGEUNITS.PX2DP (CONTEXT,&NBSP;HEIGHTDP);  }   /**  *   Get status bar height   *  @param  context  context   *  @return   status bar height   unit pixels   */ public static int getstatusbarheightpixel (Activity context) {        &nbsP; class<?> 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 = context.getresources (). Getdimensionpixelsize (x);           } catch  (EXCEPTION&NBSP;E1)  {              e1.printstacktrace ();          }           return statusBarHeight;      }   /**  *  Get status bar height   *  @param  context  Context   *  @return   status bar height   unit dp  */ public static int  GETSTATUSBARHEIGHTINDP (activity context) {         class<? > 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 =  Context.getresources (). getdimensionpixelsize (x);           }  catch  (EXCEPTION&NBSP;E1)  {              e1.printstacktrace ();         }          &NBSP;RETURN&NBSP;CHANGEUNITS.PX2DP (context, statusbarheight);      }}

This article is from the "sharing is the best memory" blog, please be sure to keep this source http://elyar.blog.51cto.com/9864306/1740025

Android Share notes (1) Get screen size, including status bar

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.