Android gets common helper methods (get screen height, width, density, notice bar height, screenshot) _android

Source: Internet
Author: User
Tags object object

We need to get the physical dimensions of the Android phone or pad screen to facilitate the design of the interface or the implementation of other functions. Let's share some of the most common ways to help Android:

Get screen Height:

/**
* Get screen height
* @param context
* @return
* by Hankkin at:2015-10-07 21:15:59/public
static int getscreenwidth {
WindowManager wm = (WindowManager) context
. Getsystemservice ( Context.window_service);
Displaymetrics outmetrics = new Displaymetrics ();
Wm.getdefaultdisplay (). Getmetrics (outmetrics);
return outmetrics.widthpixels;
}

Get screen width:

/**
* Get screen width
* @param context
* @return
* by Hankkin at:2015-10-07 21:16:13/public
static int getscreenheight {
WindowManager wm = (WindowManager) context
. Getsystemservice ( Context.window_service);
Displaymetrics outmetrics = new Displaymetrics ();
Wm.getdefaultdisplay (). Getmetrics (outmetrics);
return outmetrics.heightpixels;
}

Get screen density:

/** *
Get screen density
* @param context
* @return
* by Hankkin at:2015-10-07 21:16:29
/Public Static float getscreendensity {return
context.getresources (). Getdisplaymetrics (). density;
}

Dip turn px:

/**
* dip to px pixel
* @param context
* @param px
* @return
* by Hankkin at:2015-10-07 21:16:43 *
* public
static int dip2px (context, float px) {
final float scale = getscreendensity (context);
return (int) (PX * scale + 0.5);

To get the status bar height:

/**
* Gets the height of the status bar
* @param context
* @return
* by Hankkin at:2015-10-07 21:16:43
/Public static int Getstatusheight (context context) {
int statusheight =-1;
try {
class<?> clazz = Class.forName ("Com.android.internal.r$dimen");
Object object = Clazz.newinstance ();
int height = integer.parseint (Clazz.getfield ("Status_bar_height")
. Get (object). toString ());
Statusheight = Context.getresources (). getdimensionpixelsize (height);
} catch (Exception e) {
e.printstacktrace ();
}
return statusheight;
}

Get screen Current screenshot:

/** * Get current screen capture, including status bar * @param activity * @return * by Hankkin at:2015-10-07 21:16:43/public static Bitmap Snapshotwith
StatusBar (activity activity) {View view = Activity.getwindow (). Getdecorview (); view.setdrawingcacheenabled (true);
View.builddrawingcache ();
Bitmap bmp = View.getdrawingcache ();
int width = getscreenwidth (activity);
int height = getscreenheight (activity);
Bitmap BP = null;
bp = bitmap.createbitmap (BMP, 0, 0, width, height);
View.destroydrawingcache ();
return BP; /** * Gets the current screen capture, does not contain status bar * @param activity * @return * by Hankkin at:2015-10-07 21:16:43/public static Bitmap Snapshotwi Thoutstatusbar (activity activity) {View view = Activity.getwindow (). Getdecorview (); View.setdrawingcacheenabled (True
);
View.builddrawingcache ();
Bitmap bmp = View.getdrawingcache ();
Rect frame = new Rect ();
Activity.getwindow (). Getdecorview (). Getwindowvisibledisplayframe (frame);
int statusbarheight = Frame.top;
int width = getscreenwidth (activity); int height = getscreenheight (activity);
Bitmap BP = null;
bp = bitmap.createbitmap (BMP, 0, statusbarheight, width, height-statusbarheight);
View.destroydrawingcache ();
return BP; }

The above is described in this article for the introduction of Android to obtain commonly used auxiliary methods (get screen height, width, density, notice bar height, screenshots), I hope for everyone is also help, more information to log on cloud community website to learn more information.

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.