问题
I have created an avd of Xiomi Redmi 3s (294dpi), whose specifications are:
The generalized densities in Android are:
- Ldpi (low) ~ 120 dpi -> dp = px * 0.74
- Mdpi (average) ~ 160 dpi -> dp = px * 1
- Hdpi (high) ~ 240 dpi -> dp = px * 1.5
- Xhdpi (extraalta) ~ 320 dpi -> dp = px * 2
- Xxhdpi (extra extraalta) ~ 480 dpi -> dp = px * 3
- Xxxhdpi (extra extra extraalta) ~ 640 dpi -> dp = px * 4
When I create the avd with device specifications, the assigned density is xhdpi (x2), but the actual device density factor is less than x2, as with most devices, so the preview result does not match the actual appearance of the device screen. For example, the same thing happens with the Nexus 5x, which has the resource density of xxhdpi (x3), but the actual density is 2.63
In Android Studio, emulated devices such as Nexus 5x or Pixel XL, specifies the special density of 420dpi and 560dpi respectively, where the density factor is 2.6 (xxhdpi) and 3.5 (xxxhdpi) approximately.
To determine the width dp and its density factor of the Xiomi Redmi 3S I have used:
dpi/160 = densityPxValue 294/160 = 1,8375
and I calculated the real density with https://stackoverflow.com/a/5104104/4114846
The result is the density of 294 dpi and the actual density factor is 1.83, but the resource folder assigned by the system is xhdpi (x2) and the preview corresponds to the generalized density.
How can I create an avd and preview by specifying the density of the device or its density factor so that it looks like the actual device ?
If you can not specify with android studio, what alternatives do we have?
回答1:
Find your AVD folder.
C:\users\<user>\.android\avd
Open the folder for the device and edit line:
hw.lcd.density=320
in
config.ini
While you have calculated 294 ppi, 294 is not allowed. Allowed values are:
120, 160, 213, 240, 280, 320, 360, 400, 420, 480, 560, 640
Please note, that those are densities:
0.75, 1, 1.33, 1.5, ...
Please note that 1.33 does not fit the Android list of ldpi, mdpi, hdpi, ...
It was needed so there was a value between mdpi and hpdi. I suspect there was hardware which had 213 ppi displays but hardward specs routinely change. Phones today are getting bigger while keeping the same pixel count, so a 4.5", 213 ppi phone from a few years ago has been replaced by a 5", 196 ppi phone.
The hardware factory sets the logical density at whatever number they like but if sensible they set it as close to the physical density as possible. So, a device will be set at 213 even though it's really 196.
Unfortunately, this leads to problems like you have encountered. Since you say the physical screen size gives a resolution of 294 ppi it is likely the hardware was set at density 1.75 (280 ppi). This is 5% less than the physical ppi but it is common for devices to report logical densities that different to physical densities (for example, 213 is 8% larger than 196).
Because 280 is exactly half way between 240 hdpi and 320 xhdpi I don't know how it will scale views. The emulator may render your layout perfectly but it may be using drawables from the hdpi folder. Your client's hardware may select drawables from the xhdpi folder which will scale them very differently.
来源:https://stackoverflow.com/questions/43710392/how-to-create-an-avd-with-android-studio-to-preview-a-real-device