简体   繁体   中英

How to reliably calculate the screen size / screen zoom level when you can set different screen resolutions?

Is there a way to reliably calculate the screen size / screen zoom level, which can be changed in Settings App -> Display?

I found here that you can use DisplayMetrics.DENSITY_DEVICE_STABLE and densityDpi to calculate the "zoom level". This generally works for most devices, but not on certain Samsung devices where you can change the resolution.

On a Samsung Galaxy S10 and the resolution set to FHD+:

  • DENSITY_DEVICE_STABLE reports 420
  • standard display zoom: densityDpi reports 420
  • next zoom level: densityDpi reports 450

420 / 420 = 1

450 / 420 = 1.0714285

Works as expeced, yay!

Now we change the resolution:

Samsung Galaxy S10 and the resolution set to WQHD+:

  • DENSITY_DEVICE_STABLE reports 420
  • standard display zoom: densityDpi reports 560

560 / 420 = 1.3333334

We can't reliably calculate the zoom level since densityDpi reports different values for the same zoom level on different resolutions, but DENSITY_DEVICE_STABLE stays the same, regardless of what resolution is set.

Is there any other way you can do this?

I think I found the "issue" myself. It seems like you have to handle density changes for your activity yourself. When you add the following line to your manifest, then the app UI is resized according to the density and doesn't stay the same. So the values for DisplayMetrics.DENSITY_DEVICE_STABLE and resources.displayMetrics.densityDpi are both correct, but not visible in the UI, since the system handles scaling automatically.

Just add

android:configChanges="density|fontScale"

and change the resolution. Now you should be able to see that the UI is scaled "correctly".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM