Android obtains the screen width and height, and android obtains the control width and height.
1. Get screen width and height
Method 1:
Int screenWidth = getWindowManager (). getdefadisplay display (). getWidth (); // screen width (pixels, for example, 480px) int screenHeight = getWindowManager (). getdefadisplay display (). getHeight (); // screen height (pixel, for example, 800 p) Log. e (TAG + "getDefaultDisplay", "screenWidth =" + screenWidth + "; screenHeight =" + screenHeight );
Method 2:
DisplayMetrics dm = new DisplayMetrics (); dm = getResources (). getDisplayMetrics (); float density = dm. density; // screen density (pixel ratio: 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, for example, 480px) screenHeight = dm. heightPixels; // The screen height (pixels, for example, 800px) Log. e (TAG + "DisplayMetrics (111)", "screenWidth =" + screenWidth + "; screenHeight =" + screenHeight );
Method 3:
Dm = new DisplayMetrics (); getWindowManager (). getdefadisplay display (). getMetrics (dm); density = dm. density; // screen density (pixel ratio: 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, for example, 320dip) int screenHeightDip = dm. heightPixels; // screen width (dip, for example, 533dip) Log. e (TAG + "DisplayMetrics (222)", "screenWidthDip =" + screenWidthDip + "; screenHeightDip =" + screenHeightDip); screenWidth = (int) (dm. widthPixels * density + 0.5f); // screen width (px, for example: 480px) screenHeight = (int) (dm. heightPixels * density + 0.5f); // The screen height (px, for example, 800px) Log. e (TAG + "DisplayMetrics (222)", "screenWidth =" + screenWidth + "; screenHeight =" + screenHeight );
2. Obtain the width and height of the Control. Generally, the width and height of the control we obtained in onCreate are 0. The following method is used to obtain the true width and height.
Method 1:
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 loads onMeasure three times
Method 2:
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 onMeasure twice, but the callback function calls back multiple times.
Method 3:
ViewTreeObserver vto2 = imageView.getViewTreeObserver(); vto2.addOnGlobalLayoutListener(new OnGlobalLayoutListener() { @Override public void onGlobalLayout() { imageView.getViewTreeObserver().removeGlobalOnLayoutListener(this); textView.append("\n\n"+imageView.getHeight()+","+imageView.getWidth()); } });
This method loads onMeasure twice, but the callback function only calls back once.