Android Programming gets screen width and gain control wide-height method _android

Source: Internet
Author: User

The example in this article describes how the Android program gets the width of the screen and the height of the gain control. Share to everyone for your reference, specific as follows:

Get Screen width height

Gets the screen width (method 1) int screenwidth = Getwindowmanager (). Getdefaultdisplay (). GetWidth (); Screen width (pixels, such as: 480px) int screenheight = Getwindowmanager (). Getdefaultdisplay (). GetHeight (); High screen (pixel, such as: 800p) log.e (TAG + "Getdefaultdisplay", "screenwidth=" + ScreenWidth +;
screenheight= "+ screenheight);
Get screen density (method 2) displaymetrics DM = new Displaymetrics ();
DM = Getresources (). Getdisplaymetrics (); float density = dm.density; Screen density (pixel scale: 0.75/1.0/1.5/2.0) int densitydpi = dm.densitydpi;
Screen density (pixels per inch: 120/160/240/320) Float xdpi = dm.xdpi;
float ydpi = dm.ydpi; LOG.E (TAG + "Displaymetrics", "xdpi=" + xdpi + ";
Ydpi= "+ ydpi); LOG.E (TAG + "Displaymetrics", "density=" + density + ";
Densitydpi= "+ densitydpi); ScreenWidth = Dm.widthpixels; Screen width (pixels, such as: 480px) screenheight = Dm.heightpixels; High screen (pixel, such as: 800px) log.e (TAG + "Displaymetrics", "screenwidth=" + ScreenWidth + ";
screenheight= "+ screenheight);
Gets the screen density (method 3) DM = new Displaymetrics (); Getwindowmanager (). Getdefaultdisplay (). getmetricS (DM); density = dm.density; Screen density (pixel scale: 0.75/1.0/1.5/2.0) densitydpi = dm.densitydpi;
Screen density (pixels per inch: 120/160/240/320) xdpi = dm.xdpi;
ydpi = dm.ydpi; LOG.E (TAG + "Displaymetrics", "xdpi=" + xdpi + ";
Ydpi= "+ ydpi); LOG.E (TAG + "Displaymetrics", "density=" + density + ";
Densitydpi= "+ densitydpi); int screenwidthdip = Dm.widthpixels; Screen width (dip, such as: 320dip) int screenheightdip = Dm.heightpixels; Screen width (dip, such as: 533dip) log.e (TAG + "displaymetrics (222)", "screenwidthdip=" + Screenwidthdip + ";
screenheightdip= "+ Screenheightdip); ScreenWidth = (int) (Dm.widthpixels * density + 0.5f); Screen width (px, such as: 480px) screenheight = (int) (Dm.heightpixels * density + 0.5f); High screen (PX, such as: 800px) log.e (TAG + "displaymetrics (222)", "screenwidth=" + ScreenWidth + ";

 screenheight= "+ screenheight);

Gets the height of the control

In general, the width of the controls we get in the OnCreate is 0. The following method can be used to get the true width and height

Method One:

int w = View.MeasureSpec.makeMeasureSpec (0,view.measurespec.unspecified);
int h = View.MeasureSpec.makeMeasureSpec (0,view.measurespec.unspecified);
Imageview.measure (W, h);
int height =imageview.getmeasuredheight ();
int width =imageview.getmeasuredwidth ();
Textview.append ("\ n" +height+ "," +width);

This method will load onmeasure three times

Method Two:

Viewtreeobserver vto = Imageview.getviewtreeobserver ();
Vto.addonpredrawlistener (New Viewtreeobserver.onpredrawlistener () {public
  Boolean Onpredraw () {
    int Height = imageview.getmeasuredheight ();
    int width = imageview.getmeasuredwidth ();
    Textview.append ("\ n" +height+ "," +width);
    return true;
  }
});

This method loads the onmeasure two times, but the callback function recalls many times

Method Three:

Viewtreeobserver Vto2 = Imageview.getviewtreeobserver ();
Vto2.addongloballayoutlistener (New Ongloballayoutlistener () {
  @Override public
  void Ongloballayout () {
    Imageview.getviewtreeobserver (). Removeglobalonlayoutlistener (this);
    Textview.append ("\ n" +imageview.getheight () + "," +imageview.getwidth ());
  }
});

This method loads onmeasure two times, but the callback function only recalls one

I hope this article will help you with the 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.