I have a custom view.
When I draw a line from (x,y) to (x+10,y), is the distance in DIP or PX?
As documentation says,
At runtime, the system transparently handles any scaling of the dp
units, as necessary, based on the actual density of the screen in use.
Thus, all units are in dp.
If you are using one of the Canvas.drawLine methods it is in PX
I believe it's in pixels. If you want to know how to convert from DP to pixels as in if you want your custom view to work with DPs just like all other Views.
Converting pixels to dp
Related
I am making a calendar and I need a grid. Problem is the borders, or rather the space between each grid, which is what I am using to kind of simulate a grid, is 1 dp. But its rather thick. I am looking at other calendar apps that have the borders, and they are very thin. Even if I were to use a drawable shape and make it 1dp, it still has that thickness. I tried using .5, but that did not seem to work. Is that not possible?
Android does allow you to enter fractional values for dp into XML, though I'm not sure I would recommend it because the results become less easy to predict/compute. Devices convert dp values into pixels using (basically) the following formula:
px = (int)(scale * dp + 0.5)
(i.e. the device density scale rounded to the nearest whole pixel value)
The scale value would be based on the screen density of the device:
MDPI = 1
HDPI = 1.5
XHDPI = 2
XXHDPI = 3
So 0.5dp would result in {1px, 1px, 1px, 2px} for the above densities, whereas 1dp would be {1px, 2px, 2px, 3px}. A tiny value like 0.1dp would resolve to {1px, 1px, 1px, 1px} because anything less than 1 in the above formula resolves to a single pixel unless the value was explicitly 0dp (it's not possible to draw something thinner than a single pixel width).
If you want to ensure that thinnest possible width is used, probably best to define the width explicitly with px instead of a scaled unit like dp. Setting a value of 1px will draw with a single pixel on all devices, and is much more readable than hoping 0.5dp or 0.1dp does the same.
Yes.
I set to 0.1dp and it is smaller than 1dp on my 4" ME860 DRIOD.
Simple answer is yes, you can.
Simpe solution to your problem (i.e. achieve the thinnest possible line) set thinckness to:
1px
I suggest px as its not device dependant and it is the smallest measure available, furthermore it wont be converted into another form, therefore no rounding errors nor any unexpected rendering... let me explain.
Android layout xml file, or dimensions xml values file, will allow you to enter decimal values for dp.
HOWEVER:
I have experimented with several values from 0.1dp to 1dp, on many devices. Depending on the device - it may not render so as you expect.
On lower pixel density devices, the lines may render thicker on one side compared to another side even though they are coded to have the same... This is due to inaccuracy introduced when truncating the converted to dp to px value - (as Devunwired mentions):
px = (int)(scale * dp + 0.5)
A pixel (px) is the smallest unit and must be an integer. so - if your aim is to simply have the smallest possible line/border, why not set it to:
1px
I hope this helps!
i'm trying to understand if the margin values of the Relativelayout in android are in pixels or dpi ?
thanks.
In xml you specifies measuring units such as android:layout_marginTop="13dp". See documentation about measuring units.
In code you specifies masuring units in pixels (for example margins), but you can read dimensions from resources, where you can specify dimension in dp or sp, etc.
You can use both pixels and dpi. But if you want to make your application for multiple screen support then you have to prefer dpi. Because it will adjust the size according to screen size.
You can choose. Enter either "5px" or "5dp", and it will adapt itself.
I guess you are refering to the values of certain methods. they are in pixels so you better convert the dpi values to pixels using displaymetrics
I want to specify an animation resource to use with Activity.overridePendingTransition. However the only available dimensions for the animations are %ages, pixels, etc but not inches. I need to use inches in my animation. Is there a workaround to this
There's two ways to do this. The easiest (presumably, without seeing your code) is to define the number of DPs that you want for the animation in a dimension xml resource. You can then get this value converted into "local" pixels by calling getResources().getDimensionPixelSize(). That will get you what you want. Alternatively you can use the formula to convert from DPs to PX manually: px = dp * (dpi / 160)
I am confused about android's multiple screen support. http://developer.android.com/guide/practices/screens_support.html I read this article, but still something is not clear for me.
I used dp instead of px in my layout
I put high, medium, low version's of an image to drawable directories.
I made this changes according to this article. But in some densities, there is still problem although some of them work very well.
The question is what is the exact width and height in dp units for variety of android screen types. if it is changeable, what is the difference between px?
px is changeable, dp is changeable too??? what is the difference??
if changeable, should I change the view's width and height by code on Create function or create seperate layouts for each screen dentisies? Please give a way to understand this...
Thanks in advance..
px are not changeable. dps or dips are.
To calculate how many px your object specified in dps will be use the formula below:
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.
px is a fixed measure. This means that if 100px on a small screen take up 1/2 of the screen, it will take up much less on a large screen. dp = density (independent) pixels, is based on the densitity of the device. So if you specify a width to 50dp on a small screen, it will expand on a large screen. Note that dp is not an insurance of layout compability on all devices, since devices have different aspect ratios. To build a perfect layout, that looks exactly the same on all devices, you must use more techniques. Linearlayout allows you to assign weights. Look into that. http://developerlife.com/tutorials/?p=312
i am new to android..can any one tell when we need to go for size in terms of ps insted of px?..thanks in advance
measurement types:
px
Pixels - corresponds to actual pixels on the screen.
in
Inches - based on the physical size of the screen.
mm
Millimeters - based on the physical size of the screen.
pt
Points - 1/72 of an inch based on the physical size of the screen.
dp
Density-independent Pixels - an abstract unit that is based on the physical density of the screen. These units are relative to a 160 dpi screen, so one dp is one pixel on a 160 dpi screen. The ratio of dp-to-pixel will change with the screen density, but not necessarily in direct proportion. Note: The compiler accepts both "dip" and "dp", though "dp" is more consistent with "sp".
sp
Scale-independent Pixels - this is like the dp unit, but it is also scaled by the user's font size preference. It is recommend you use this unit when specifying font sizes, so they will be adjusted for both the screen density and user's preference.
source: http://developer.android.com/guide/topics/resources/more-resources.html#Dimension
fill_parent attribute is used expand to fill any remaining space in the parent view...
Generally, you should use density-independent units (like dp) instead of pixels. There's a detailed guide with examples here.
Firstly, do you mean 'pt' (or Points) instead of ps?
Px is short for pixels - use this if you want to be sure something is an absolute number of pixels in size. 'pt' is a publish measure meaning '1/72' of an inch. As this is a real-world measurement, it is not tied to pixels. Pixels can be of different sizes on different devices at different resolutions.
So, if you want to make sure something is of a certain real-world physical size, use 'pt' but if you want to make sure it is a set number of pixels, use px.