标签云

微信群

扫码加入我们

WeChat QR Code

What is the difference between Android units of measure?pxdipdpsp


this is use full difference between px, dip, dp and sp in android [ developer.android.com/guide/topics/resources/…

2019年06月26日44分59秒

This nifty converter demonstrates it best, in my opinion. It's also extremely useful for exporting sprites from Photoshop or designing your layout for a physical dimension.

2019年06月26日44分59秒

new description on Google Design Units and measurements

2019年06月26日44分59秒

How to programmatically convert between px, dp, and sp

2019年06月26日44分59秒

from android developers site developer.android.com/guide/practices/screens_support.html

2019年06月25日44分59秒

even though they should be the same thing I tend to have lots of problems with dp while everything works fine with dip

2019年06月26日44分59秒

One note about db/sp that isn't totally obvious: The scaling that occurs for these depends not on the devices real density (dpi) but on which "bucket" of densities it falls into: available buckets are: 120,160,240,320. This can cause some problems handling screens that are significantly different but get bucketed the same.

2019年06月26日44分59秒

Note that the documentation no longer mentions "dip" at all, just "dp", although the compiler still seems to accept "dip".

2019年06月26日44分59秒

android_developer (5 comments above) dp does not have the exact same physical length. (Although it is close.) See Fraggle's comment about bucketing. What this means is that 48dp will be roughly 8mm (0.3 inch), but it may vary up to 11mm.

2019年06月26日44分59秒

The reason for bucketing is so that developers can test their apps on a few devices of different densities and be confident the the layouts will look the same on a multitude of devices. So even if the physical size of buttons etc changes a little bit, the overall look of an activity will be the same.

2019年06月26日44分59秒

So if you use dp for a button and sp for the font size of the button text, what happens when the user starts scaling? The text will enlarge, but will the button accommodate this by enlarging also?

2019年06月26日44分59秒

Wytze, nope.For things where fitting the text may be an issue, I would just use dip so things don't overflow.

2019年06月26日44分59秒

Wytze And I, on the contrary, use sp for both the text and whatever it contains. For example, if I have a button with a fixed size and a text inside it, I would assign the button size in sp so it scales up when needed too. Otherwise, the user will be annoyed that he increased the size of the text, and it didn't increase. Of course, the layout should be flexible enough to allow it.

2019年06月26日44分59秒

If your designers won't leave room for text-based elements to scale according to sp size, one option is to switch to whatever layout you'd use for a narrower device, then scale everything up uniformly by (DisplayMetrics.scaledDensity/DisplayMetrics.density).

2019年06月26日44分59秒

Sam lack of any free space will also cause problems during i18n

2019年06月26日44分59秒

How do we calculate the pixels for hdpi,ldpi,mdpi etc.. I heard we use this formula..px = dp * ( dpi / 160 ); can you explain me with this formula plz ?

2019年06月26日44分59秒

Rakeshpatanga At a density of 160 dpi, 1 px = 1 dp (the physical pixel and dp abstraction are the same). That is, for a single dp we have (1)*(160/160) = 1, or exactly 1 px. At the higher density of 320 dpi, a single dp is (1)*(320/160) = 2 pixels, and two dp's are (2)*(320/160) = 4 px.

2019年06月26日44分59秒

Line these up in monospace to see: |1dp|, |___|___| 160, |_|_|_|_|320 (put line breaks after the commas). |_| = 1 pixel.

2019年06月26日44分59秒

re "You'll need to create different size images for supported screen sizes:"That depends. For small high-contrast images, e.g. icons, it is wise to create at least the smallest size (to be sure it will be clear) plus a fairly large size (so that large versions don't look blurry). But even for icons, you can allow one image to scale over a range of sizes, at higher densities. For photographs, just specify the display area you want it to fill, and rely on the device scaling.

2019年06月26日44分59秒

It should be ppi instead of dpi

2019年06月26日44分59秒

Also: what is the added value of this answer? There doesn't seem to be anything that hasn't already been mentioned in other answers.

2019年06月26日44分59秒

getDimensionPixelOffset rounds down, getDimensionPixelSize rounds up.

2019年06月26日44分59秒

EnesBattal, I think because the dp isn't an acute physical size, it is a approximate value. Quoting from CapTech : "dp - This is a density independent unit, however the physical size of a single “dp” is only approximately the same on every screen density. There are approximately 160 “dp” in an inch. A scaling factor, depending on the density bucket of the device, is applied to convert “dp” to the number of pixels at 160 dpi. The number of pixels a single “dp” translates to varies depending on the pixel on screen density and the density bucket the device falls into."

2019年06月26日44分59秒

RuchirBaronia, I think the DP or DIP is still there inside apk, because the apk doesn't know which kind of screen density it will run with yet, so the device independence should be still kept.

2019年06月26日44分59秒

You know that the font scale factor applied to sp is a factor, right? Anything that affects dp will also affect sp. That said, it's still preferable to specify font sizes using dp instead of sp if your layout is very tight and the larger sizes won't fit - better to have text smaller than what the user wants that a completely messed up layout. But in the first instance you should always strive to respect the user's font size preference - even the biggest setting is not THAT big.

2019年06月26日44分59秒

Very good tutorial for understanding is : http:/vinsol.com/blog/2014/11/20/tips-for-designers-from-a-developer

2019年06月26日44分59秒

"We should avoid to use sp" Why is that? You should use sp when dealing with font sizes because it takes into account the user's preferred text size, developer.android.com/training/multiscreen/screendensities.html

2019年06月26日44分59秒

I have answered relative to layout perspective.. Please read on link you provided "so you should use this measurement unit when defining text size (but never for layout sizes)."

2019年06月26日44分59秒

The question was "Difference between px, dp, dip and sp in Android?" your answer said "We should avoid to use sp". There was no mention of "layout perspective" anywhere.

2019年06月26日44分59秒

Yes.. dp and dip are same... used interchangeably... Is my answer solve your query?

2019年06月26日44分59秒

As per the documentationThe compiler accepts both "dip" and "dp", though "dp" is more consistent with "sp”.

2019年06月26日44分59秒

you confused with the Phone with low screen resolution to the appropriate image

2019年06月26日44分59秒

RoyiNamir Could you please explain your comment, or if possible please suggest edit

2019年06月26日44分59秒

this is a phone with HIgh resolution. not low : i.imgur.com/tnu87VR.jpg

2019年06月26日44分59秒