您好,登錄后才能下訂單哦!
1.獲取屏幕寬高
方法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);
2.獲取控件的寬高,一般來(lái)說(shuō),我們?cè)趏nCreate里面得到的控件的寬高全是0.采用下面的方法,可以得到真實(shí)的寬高
方法一 :
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);
此方法會(huì)加載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; } });
此方法會(huì)加載onMeasure二次,但是回調(diào)函數(shù)會(huì)回調(diào)很多次
方法三:
ViewTreeObserver vto2 = imageView.getViewTreeObserver(); vto2.addOnGlobalLayoutListener(new OnGlobalLayoutListener() { @Override public void onGlobalLayout() { imageView.getViewTreeObserver().removeGlobalOnLayoutListener(this); textView.append("\n\n"+imageView.getHeight()+","+imageView.getWidth()); } });
此方法會(huì)加載onMeasure二次,但是回調(diào)函數(shù)只回調(diào)一次
以上就是本文的全部?jī)?nèi)容,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來(lái)一定的幫助,同時(shí)也希望多多支持億速云!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。