Android編程擷取螢幕寬高與擷取控制項寬高的方法_Android

來源:互聯網
上載者:User

本文執行個體講述了Android編程擷取螢幕寬高與擷取控制項寬高的方法。分享給大家供大家參考,具體如下:

擷取螢幕寬高

// 擷取螢幕寬高(方法1)int screenWidth = getWindowManager().getDefaultDisplay().getWidth(); // 螢幕寬(像素,如:480px)int screenHeight = getWindowManager().getDefaultDisplay().getHeight(); // 螢幕高(像素,如:800p)Log.e(TAG + " getDefaultDisplay", "screenWidth=" + screenWidth + "; screenHeight=" + screenHeight);// 擷取螢幕密度(方法2)DisplayMetrics dm = new DisplayMetrics();dm = getResources().getDisplayMetrics();float density = dm.density; // 螢幕密度(像素比例:0.75/1.0/1.5/2.0)int densityDPI = dm.densityDpi; // 螢幕密度(每寸像素: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; // 螢幕寬(像素,如:480px)screenHeight = dm.heightPixels; // 螢幕高(像素,如:800px)Log.e(TAG + " DisplayMetrics(111)", "screenWidth=" + screenWidth + "; screenHeight=" + screenHeight);// 擷取螢幕密度(方法3)dm = new DisplayMetrics();getWindowManager().getDefaultDisplay().getMetrics(dm);density = dm.density; // 螢幕密度(像素比例:0.75/1.0/1.5/2.0)densityDPI = dm.densityDpi; // 螢幕密度(每寸像素: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; // 螢幕寬(dip,如:320dip)int screenHeightDip = dm.heightPixels; // 螢幕寬(dip,如:533dip)Log.e(TAG + " DisplayMetrics(222)", "screenWidthDip=" + screenWidthDip + "; screenHeightDip=" + screenHeightDip);screenWidth = (int)(dm.widthPixels * density + 0.5f); // 螢幕寬(px,如:480px)screenHeight = (int)(dm.heightPixels * density + 0.5f); // 螢幕高(px,如:800px)Log.e(TAG + " DisplayMetrics(222)", "screenWidth=" + screenWidth + "; screenHeight=" + screenHeight);

擷取控制項的寬高

一般來說,我們在onCreate裡面得到的控制項的寬高全是0.採用下面的方法,可以得到真實的寬高

方法一:

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);

此方法會載入onMeasure三次

方法二:

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;  }});

此方法會載入onMeasure二次,但是回呼函數會回調很多次

方法三:

ViewTreeObserver vto2 = imageView.getViewTreeObserver();vto2.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {  @Override  public void onGlobalLayout() {    imageView.getViewTreeObserver().removeGlobalOnLayoutListener(this);    textView.append("\n\n"+imageView.getHeight()+","+imageView.getWidth());  }});

此方法會載入onMeasure二次,但是回呼函數只回調一次

希望本文所述對大家Android程式設計有所協助。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.