I know this might be a silly question but I have really gone through so many materiel and links but still not quite understand it. In the "Supporting Multiple Screens" section of Android Develop Doc, it introduces dp like this:
Density-independent pixel (dp)
A virtual pixel unit that you should use when defining UI layout, to express layout dimensions or position in a density-independent way. The density-independent pixel is equivalent to one physical pixel on a 160 dpi screen, which is the baseline density assumed by the system for a "medium" density screen. At runtime, the system transparently handles any scaling of the dp units, as necessary, based on the actual density of the screen in use. The conversion of dp units to screen pixels is simple: px = dp * (dpi / 160). For example, on a 240 dpi screen, 1 dp equals 1.5 physical pixels. You should always use dp units when defining your application's UI, to ensure proper display of your UI on screens with different densities.
Basically I understand the fact that screen with higher dpi would have more pixels in a single physical inch, which means a dp in such a screen would equal more physical pixels(px).
But according to the above conversion equation (in bold font), in a screen with higher dpi (e.g. 240 dpi screen) , a px = (240 / 160) * dp = 1.5dp. This seems to mean that in higher dpi screen a px would equal more dp. This looks in conflict with my previous understanding.
So, please, could anyone help me to figure this tricky issue out. Thank you a lot, really.
You are looking at the wrong place in the formula. To see how many dp equals one px in different density, lets rearrange the formula:
px = dp * (dpi/160)
dp = px / (dpi/160)
Now for 1px, in mdpi devices:
dp = 1 / (160/160) = 1dp
In hdpi devices:
dp = 1 / (240/160) = 0.666666667dp
You can see that 1px equals less dp in higher density devices
Related
I am looking at setting the FontSize of my application based on how big my device is. i.e. If the device is below 8" make the font smaller than the default.
So the device I am using is a Lenovo S8-50 8-Inch Tablet and as you can see in the specifications Screen Size: 8 inches
So looking at some code on Github I see the following:
var d = Resources.System.DisplayMetrics;
this.ScreenHeight = (int)(d.HeightPixels / d.Density);
For this device the number returned is 912 and I can't figure out how this number relates to the device.
So my question is what does this number actually represent?
From the Official documentation:
Density-independent pixel (dp)
A virtual pixel unit that you should use when defining UI layout, to express layout dimensions or position in a density-independent way.
The density-independent pixel is equivalent to one physical pixel on a 160 dpi screen, which is the baseline density assumed by the system for a "medium" density screen. At runtime, the system transparently handles any scaling of the dp units, as necessary, based on the actual density of the screen in use. The conversion of dp units to screen pixels is simple: px = dp * (dpi / 160). For example, on a 240 dpi screen, 1 dp equals 1.5 physical pixels. You should always use dp units when defining your application's UI, to ensure proper display of your UI on screens with different densities.
This will also be helpful:
Understanding density independence
I am new to android. I read about DP but m still confused. In one definition it says-
dp (density-independent pixels): An abstract unit based on the density of the screen. On a display with 160 dots per inch, 1dp = 1px.
does it mean- 160 dots=1 dp = 1 px (each dot is 1 pixel , right?)
OR 1 dp = 1 dot(pixel) among the 160 dots
Pleas clarify
density-independent pixels is a virtual pixel unit that you should use when defining UI layout, to express layout dimensions or position in a density-independent way.
The density-independent pixel is equivalent to one physical pixel on a 160 dpi screen, which is the baseline density assumed by the system for a "medium" density screen. At runtime, the system transparently handles any scaling of the dp units, as necessary, based on the actual density of the screen in use. The conversion of dp units to screen pixels is simple:
px = dp * (dpi / 160)
For example, on a 240 dpi screen, 1 dp equals 1.5 physical pixels. You should always use dp units when defining your application's UI, to ensure proper display of your UI on screens with different densities.
For 160 dpi screen 1 dp equals 1 px.
Refer to this blog and this answer.
The android documentation say -
The density-independent pixel is equivalent to one physical pixel on a 160 dpi screen, the baseline density assumed by the platform (as described later in this document). At run time, the platform transparently handles any scaling of the dp units needed, based on the actual density of the screen in use. The conversion of dp units to screen pixels is simple: pixels = dps * (density / 160). For example, on 240 dpi screen, 1 dp would equal 1.5 physical pixels. Using dp units to define your application’s UI is highly recommended, as a way of ensuring proper display of your UI on different screens.
you may take a look at the supporting multiple screens. you may also take a look at this question.
What is the formula for converting regular pixels to DIP?
Assuming I have a photoshop document with a design in it that was intended for the Galaxy Tab (for example) which is 600x1024 actual pixels.
What is the ratio between those and the DIP? I'm unclear on how Android translates this.
I want to use DIP (rather than pixels) so that it scales to look "ok" on other devices, but my primary concern is to get it pixel perfect in this resolution and my aim is to measure the position of an element in photoshop and then get an exact translation as to what the DIP needs to be for it to lay out identically on the Tab.
Their formula is somewhere in the docs:
truePixels = DIPs * (device DPI / 160)
Ah, there it is:
The density-independent pixel is
equivalent to one physical pixel on a
160 dpi screen, which is the baseline
density assumed by the system for a
"medium" density screen. At runtime,
the system transparently handles any
scaling of the dp units, as necessary,
based on the actual density of the
screen in use. The conversion of dp
units to screen pixels is simple: px =
dp * (dpi / 160). For example, on a
240 dpi screen, 1 dp equals 1.5
physical pixels. You should always use
dp units when defining your
application's UI, to ensure proper
display of your UI on screens with
different densities.
Taken from here.
im throughoutly confused by dips on Android.
I understand from the reference that the base for dp values is 160.
So, shouldn't 80dp in width equals a view with a width of 50% of the screen ?
On my Nexus One the width in dp is something around 300dp as it seems.
What am i missing here ?
thx in advance
"dp" == "Density-independent Pixels" (This is also why it was earlier called "dip", though I prefer to use "dp" these days.)
Think of it like other units -- "in" (inches), "mm" (millimeters), etc. It allows you to provide a size that is scaled based on the density of the screen.
We define mdpi to be the base density, so "10dp" on an mdpi screen will result in exactly 10 pixels. On an hdpi screen it will result in 15 pixels, because hdpi is 1.5*mdpi.
Note that though the constants for various densities are similar to DPI (mdpi is 160, etc), density is not exactly DPI. It is an abstract scaling factor that adjusts for screen dpi, but does not try to exactly reflect it. (You would use "in", "mm", etc for exact sizes but 99.9% that is not what you want so stick with "dp".) This greatly simplifies life for everyone because you don't need to deal with many Android devices having a slightly different amount of space for its UI because they each of slight different screen DPIs. Also, device manufacturers can select the density of their device to achieve a desired UI -- for example the Samsung Tab uses a density that is a fair amount larger than the actual DPI, resulting in an overall larger UI.
160 dots per inch. So 80dp would be 1/2 an inch, roughly.
I don't understand your question completely but I suggest you take a look at this, if you haven't already.
http://developer.android.com/guide/practices/screens_support.html
pixels = dps * (density / 160)
Density independent pixels (short: dp) are a virtual pixel unit that will be determined at the runtime of your application.
Formala:
1 dp = 1 Pixel on 160 dpi screen.
So 160 dpi is the baseline density for the system.
The conversion of dp units to screen pixels are quite simple.
Actual device pixels (px) = dp (1) * (dpi (of the device) / 160(baseline) )
For the sake of simplicity: px = dp * (dpi / 160)
Example:
If a 240 dpi device starts your app, then 1 dp equals to 1,5 actual device pixels.
Conclusion:
Dp automatically handles any scaling to bigger or smaller devices. The times where you hardcode the pixels are over. DP ensures the proper scaling on different screen densities.
If, in a layout xml file, I set the size to be, for example 12dip. Will that always be 12px in mdpi and so 18px in hdpi?
So is dip always true for mdpi and will scale accordingly for other densities?
That question is fully covered by official documentation. Relations between dip, px and dpi are covered by this section.
Quote:
Density-independent pixel (dp)
A virtual pixel unit that applications can use in defining their
UI, to express layout dimensions or
position in a density-independent way.
The density-independent pixel is equivalent to one physical pixel on a
160 dpi screen, the baseline density
assumed by the platform (as described
later in this document). At run time,
the platform transparently handles any
scaling of the dp units needed, based
on the actual density of the screen in
use. The conversion of dp units to
screen pixels is simple: pixels = dps
* (density / 160). For example, on 240 dpi screen, 1 dp would equal 1.5
physical pixels. Using dp units to
define your application's UI is highly
recommended, as a way of ensuring
proper display of your UI on different
screens.
So the statement:
that always be 12px in mdpi and so 18px in hdpi
seems to be correct, according to the docs.
12dp will be 12px on a device with density 160 dpi. The docs don't say that all mdpi devices have exactly 160 dpi so it seems you might find mdpi devices with other densities (e.g. 150 or 180 dpi). In those cases, the relation 1dp = 1px would not be true.
You can only be sure of this relation:
px = dp * (dpi / 160)
If a device has a density of 320 dpi then each dp corresponds to 2 px, because 320/160 is 2. I would say that 2 is the "density factor", but it is also what you get with getResources().getDisplayMetrics().density, so it is also called "density".