2017-06-13 24 views
3

我在改變顯示縮放的情況下計算Android N上的顯示尺寸時遇到了問題。使用變化的顯示縮放在Android N上獲取px的屏幕尺寸zoom

有了Android N,你就可以更改顯示縮放(檢查here
但顯示尺寸(...的getSize()),如果用戶將在Android設置的變焦並沒有改變......

任何想法解決它?我是否使用px乘以scaledDensity的大小來獲得實際的顯示大小?

我的應用程序可以在px中創建窗口和組件計算屏幕尺寸,組件在服務器上進行設計。 簡單地說我在服務器上設置寬度和智能手機屏幕的寬度數學比例:

DisplayMetrics dm = new DisplayMetrics(); 
wm.getDefaultDisplay().getMetrics(dm); 
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) { 
    display.getRealSize(sizePoint); 
}else{ 
    display.getSize(sizePoint); 
} 
int smartphoneDisplayWidth = sizePoint.x; 
int smartphoneDisplayHeight = sizePoint.y; 

int serverDisplayWidth = 720px; //this come from the server 
int serverComponentWidth = 720px; //this come from the server 
int smartphoneComponentWidth = (smartphoneDisplayWidth/serverDisplayWidth)*serverComponentWidth; 
//ex: smartphoneComponentWidth = (1080/720)*720 = 1080; the component widht on the smartphone will be 1080px. 

如果設置較小的變焦我有這樣的問題:
默認
enter image description here




Windows組件是小:
enter image description here

在PX的寬度不改變,它改變僅密度:

DisplayMetrics{density=2.2250001, width=1080, height=1813, scaledDensity=2.2250001, xdpi=422.03, ydpi=424.069} 

默認

DisplayMetrics{density=2.625,  width=1080, height=1794, scaledDensity=2.625,  xdpi=422.03, ydpi=424.069} 

DisplayMetrics{density=2.875,  width=1080, height=1782, scaledDensity=2.875,  xdpi=422.03, ydpi=424.069} 

較大

DisplayMetrics{density=3.125,  width=1080, height=1770, scaledDensity=3.125,  xdpi=422.03, ydpi=424.069} 

最大

DisplayMetrics{density=3.375,  width=1080, height=1758, scaledDensity=3.375,  xdpi=422.03, ydpi=424.069} 
+0

可能是這樣的解決方案嗎? 'int smartphoneComponentWidth =((smartphoneDisplayWidth * scaledDensity)/ serverDisplayWidth)* serverComponentWidth;' – Giovesoft

+0

放大Android N的能力超出了應用程序的範圍。另外,如果你在你的應用程序功能中只包含一個Android N,你就會限制很多可能的客戶端。 –

回答

1

您應該創建一個使用DP值,而不是PX意見。當密度變化時(如nugat設置),您會在第二個屏幕中看到結果。

也許你創建的佈局是這樣的:

int screenWidth = <value>; 
LayoutParams lp = new LayoutParams(screenWidth, 200); 
view.setlayoutparams(lp); 

應使用密度:

float sampleDensity = 2f; // get from DisplayMetrics 
int screenWidth = <value>; 
LayoutParams lp = new LayoutParams((int) (screenWidth * sampleDensity), 200); 
view.setlayoutparams(lp); 

另一個原因是,也許你不更新配置更改後的佈局。更好的解決方案是在LayoutParams中使用MATCH_PARENTWRAP_CONTENT。它更方便快捷的解決方案。

+0

是的,乘以比例密度?看看我的意見。 我不能使用MATCH和WRAP因爲應用程序是完全動態的,在Web服務器上配置,它必須在iOS和Android上工作:-) – Giovesoft

+0

乘以寬度和sampleDensity不起作用。 我認爲你必須乘以寬度和密度的差異默認和其他密度(小,大或其他)... 任何其他建議? – Giovesoft