2017-06-13 20 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