android擷取螢幕寬高與擷取控制項寬高 方法總結

來源:互聯網
上載者:User

android擷取螢幕寬高與擷取控制項寬高 方法總結

擷取螢幕 寬高 和 擷取空間的寬高 在開發的時候經常用到,剛開始做開發的時候 總是 複製粘貼 不知道 所以然 ,今天 徹底的 總結一下 擷取寬高的 用法

先講一下原理 在最後貼出代碼之一直接使用。

1. 擷取螢幕的寬高。

首先要只一個 函數就是 WindowManager

他的api是這樣說的

android.view
介面 WindowManager 所有超級介面: ViewManager
public interface WindowManagerextends ViewManager

The interface that apps use to talk to the window manager.

Use Context.getSystemService(Context.WINDOW_SERVICE) to get one of these.

Use Context.getSystemService(Context.WINDOW_SERVICE) to get one of these. 這句話很重要 因為這句話 就可以擷取螢幕空間了。

可以依照api寫出這句話 擷取螢幕的服務了。

WindowManager a = (WindowManager) getSystemService(Context.WINDOW_SERVICE); 這句怎麼用先不用著急 先放著。

還有一個函數Display

Display

結構

繼承關係

public class Display extends Object

java.lang.Object

android.view.Display

類概述

Display類提供關於螢幕尺寸和解析度的資訊。

常用方法有 getSize()目前 替代了 getWidth(),getHeight(),當然了都可以同時使用。

還有一個類 DisplayMetrics

有如下常用方法。

density; // 螢幕密度(像素比例:0.75/1.0/1.5/2.0)densityDpi; // 螢幕密度(每寸像素:120/160/240/320)widthPixels; // 螢幕寬(像素,如:480px) heightPixels; // 螢幕高(像素,如:800px)

下面開始上代碼 直接複製粘貼就行了。

方法1

/* * 將對話方塊的大小按螢幕大小的百分比設定 */WindowManager m = getWindowManager();Display d = m.getDefaultDisplay(); // 擷取螢幕寬、高用System.out.println("------------d1---------:" + d.getHeight());System.out.println("------------d1---------:" + d.getWidth());

也可以這麼寫

// 擷取螢幕寬高(方法1)  int screenWidth = getWindowManager().getDefaultDisplay().getWidth(); // 螢幕寬(像素,如:480px)  int screenHeight = getWindowManager().getDefaultDisplay().getHeight(); // 螢幕高(像素,如:800p)  

現在的版本 用getSize()代替了個頭getHeight()

這麼寫了

WindowManager a = (WindowManager) getSystemService(Context.WINDOW_SERVICE);Display d1 = a.getDefaultDisplay(); // 擷取螢幕寬、高用Point size = new Point();d.getSize(size);int width = size.x;int height = size.y;System.out.println("---------------------:" + width + height);
注意 :這個代碼不在acitiviy中寫的時候 要用
WindowManager a = (WindowManager) getSystemService(Context.WINDOW_SERVICE);


方法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 scaledDensity=dm.scaledDensity;float xdpi = dm.xdpi;float ydpi = dm.ydpi;int screenWidth = dm.widthPixels; // 螢幕寬(像素,如:480px)  int screenHeight = dm.heightPixels; // 螢幕高(像素,如:800px)  


方法3: 這個方法待定還。

DisplayMetricsdm1 = getResources().getDisplayMetrics();getWindowManager().getDefaultDisplay().getMetrics(dm1);  float density1 = dm1.density; // 螢幕密度(像素比例:0.75/1.0/1.5/2.0)  intdensityDPI1 = dm1.densityDpi; // 螢幕密度(每寸像素:120/160/240/320)  float xdpi1 = dm1.xdpi;  float ydpi1 = dm1.ydpi;  int screenWidthDip1 = dm1.widthPixels; int screenHeightDip1 = dm1.heightPixels; 

1.擷取空間的寬高 在oncreate中擷取空間寬高往往得到的是0 。下面兩種方法 擷取寬高

1.很簡單直接 複用activity方法

@Overridepublic void onWindowFocusChanged(boolean hasFocus) {// TODO Auto-generated method stubsuper.onWindowFocusChanged(hasFocus);System.out.println("dialog1:" + dialog1.getWidth());}
我基本用方法1

2.用onmeasure方法

int w = View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED);int h = View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED);btn_dialog1_2.measure(w, h);int height = btn_dialog1_2.getMeasuredHeight();int width = btn_dialog1_2.getMeasuredWidth();System.out.println("height:" + height + "width" + width);

看著就麻煩。這個文章只說明 擷取 寬高 方便使用,拿來主義

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.