I am having some problems with the dp calculation for a 7" tablet (800x480px). Below is my way of calculating. Can't find where I am making a mistake in calculation or my thought:
I have three devices that I'm testing:
- Nexus One: 800x480px, 3.7" display (especificaciones)
- Huawei Ideos: 320x240px, 2.8" display (especificaciones)
- Ainol Novo 7" Tablet: 800x480px, 7" display (especificaciones)
Based on the display size, I'm calculating the DPI (dots per inch) manually, based on the formular:
sqrt(w^2 + h^2) / in
So I am getting:
- Nexus One: sqrt(800*800+480*480)/3.7 = 252.15dpi
- Huawei Ideos: sqrt(320*230+240*240)/2.8 = 142.86dpi
- Ainol Novo 7" Tablet: sqrt(800*800+480*480)/7 = 133.28 dp
Now I want to output the dpi value of the screen metrics and based on that, the screen width and height in dp (density-independent pixels). My code for that is:
DisplayMetrics metrics = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(metrics); Log.d(TAG, "ScaledDensity: " + metrics.scaledDensity); Log.d(TAG, "Density DPI: " + metrics.densityDpi); int widthInPx = 800; // 320 or 800, depending on the respective device width I'm testing int widthInDp =(int)(widthInPx/metrics.scaledDensity); Log.d(TAG, "width in dp: " + widthInDp); int heightInPx = 480; // 240 or 480, depending on the respective device height I'm testing int heightInDp =(int)(heightInPx/metrics.scaledDensity); Log.d(TAG, "height in dp: " + heightInDp);
Los resultados son los siguientes:
- Nexus One: Scaled Density: 1.5, Density DPI: 240, Width in dp: 533, Height in dp: 320
- Huawei Ideos: ScaledDensity: 0.75, Density DPI: 120, width in dp: 426, height in dp: 320
- Ainol Novo 7" tablet: ScaledDensity: 1.0, Density DPI: 160, width in dp: 800, height in dp: 480
The density DPIs that I get from the java code inside my activity differ from my calculated dpi values and are obviously rounded by Android to one of the values 120, 160 or 240, which is quite normal. No problem so far.
With the third device, the Novo 7" tablet: The ScaledDensity is said to be 1.0, and the width in dp is said to be 800, same as the number of px. However, that's not the reality; when I create a fullscreen app and place a view on there, only 533dp x 320dp of the view actually fit on the screen. So it's the same dp measurements as the Nexus One; even though the Nexus One has a higher DPI value but the same number of pixels. How is that possible?
I create an AVD for the emulator, with the exact measurements as the Novo 7" tablet. The log lines in my java code show the same values as for the real tablet device:
- Ainol Novo 7" tablet Emulador: ScaledDensity: 1.0, Density DPI: 160, width in dp: 800, height in dp: 480
However, on the emulator, I can place a view with 580dp x 320 dp to to fully visible in a fullscreen activity. How is it possible that the width dp differs between the emulator and the real device with exact same measurements?
To help you target some of your designs for different types of
devices, here are some numbers for typical screen widths:
480dp: una tableta tweener como la Streak (480x800 mdpi).
600dp: una tableta de 7 ”(600x1024 mdpi).
I assume my tablet should fall under one of these categories, however, neither does it have 480 width nor 600 dp width, but something in between; in my case, either 533dp (real device) or 580dp (emulator).
If this is the case, what good does such a 'typical screen widths' overview do, if in reality, it doesn't actually seem to be the case but devices from different vendors can have any other random value in between the standard ones? Then I could just go ahead and back to working with pixel values again, as the entire benefit of using dp seems to be absent.
preguntado el 02 de febrero de 12 a las 11:02
Argh, I simply forgot the line
<uses-sdk android:minSdkVersion="7" android:targetSdkVersion="11" />
en mi manifiesto.
Without android:minSdkVersion, the are always problems; legacy issues with Android 1.5 I think.