我在Android N上更改了显示缩放比例,但计算显示尺寸时遇到了问题。
在Android N上,您可以更改显示缩放比例(请参见此处),但如果用户从Android设置中设置缩放比例,则显示大小(...getSize())不会改变...
有什么解决办法吗?我需要使用以px为单位乘以scaledDensity的尺寸才能得到实际的显示尺寸吗?
我的应用程序动态创建窗口和组件,通过计算像素屏幕尺寸来设计组件。这些组件是在服务器上设计的。简而言之,我通过在服务器上设置的宽度与智能手机屏幕宽度之间进行数学比例计算:
如果我设置一个较小的缩放比例,我就会遇到这个问题:
默认:
缩小后:
窗口组件变得太小了:
在Android N上,您可以更改显示缩放比例(请参见此处),但如果用户从Android设置中设置缩放比例,则显示大小(...getSize())不会改变...
有什么解决办法吗?我需要使用以px为单位乘以scaledDensity的尺寸才能得到实际的显示尺寸吗?
我的应用程序动态创建窗口和组件,通过计算像素屏幕尺寸来设计组件。这些组件是在服务器上设计的。简而言之,我通过在服务器上设置的宽度与智能手机屏幕宽度之间进行数学比例计算:
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.
如果我设置一个较小的缩放比例,我就会遇到这个问题:
默认:
缩小后:
窗口组件变得太小了:
宽度以像素为单位不变,只有密度发生了变化:
缩小
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}
int smartphoneComponentWidth = ((smartphoneDisplayWidth*scaledDensity) / serverDisplayWidth) * serverComponentWidth;
- Giovesoft