Android gets the phone's screen width, status bar height, and string width-high information method _android

Source: Internet
Author: User
Tags pow

The example in this article describes Android's approach to getting mobile screen width, status bar height, and string width-high information. Share to everyone for your reference. Specifically as follows:

First define the TextView object Commenttext

Get the width of the text:

Textpaint textpaint = new Textpaint (paint.anti_alias_flag);
Textpaint.settextsize (Commenttext.gettextsize ());
Textpaint.setcolor (color.white);
FontMetrics FontMetrics = Textpaint.getfontmetrics ();
float ftop = fontmetrics.top;
float fbottom = fontmetrics.bottom;
float textHeight = (int) (fbottom-ftop);
float textWidth = (int) textpaint.measuretext (Commenttext.gettext ());

Get the status bar height above the phone screen:

Copy Code code as follows:
Displaymetrics dm = new Displaymetrics ();
Getwindowmanager (). Getdefaultdisplay (). Getmetrics (DM);
int width = dm.widthpixels; Screen width
int height = dm.heightpixels; High screen
Rect frame = new Rect ();
GetWindow (). Getdecorview (). Getwindowvisibledisplayframe (frame);
int statusbarheight = Frame.top; Status bar Height
int contenttop = GetWindow (). Findviewbyid (window.id_android_content). GetTop ();
int titlebarheight = Contenttop-statusbarheight; High title bar

Get Phone screen width:

Copy Code code as follows:
WindowManager wm = (WindowManager) this.getsystemservice (Context.window_service);
int width = Wm.getdefaultdisplay (). getwidth ();//Screen width
int height = Wm.getdefaultdisplay (). GetHeight ();//Screen height

Get TextView width

Textpaint paint = Textview.getpaint ();
float len = Paint.measuretext (string);

To get the screen size:

Displaymetrics dm = new Displaymetrics (); 
Getwindowmanager (). Getdefaultdisplay (). Getmetrics (DM); 
Double x = Math.pow (dm.widthpixels/dm.xdpi,2); 
Double y = Math.pow (dm.heightpixels/dm.ydpi,2); 
Double screeninches = math.sqrt (x+y); Screen size (inches)

I hope this article will help you with your Android program.

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.