标签云

微信群

扫码加入我们

WeChat QR Code


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

2018年12月16日23分56秒

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.

2018年12月16日23分56秒

new description on Google Design Units and measurements

2018年12月15日23分56秒

Difference between DP, SP And Pixels with examples.

2018年12月16日23分56秒

How to programmatically convert between px, dp, and sp

2018年12月15日23分56秒

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

2018年12月16日23分56秒

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.

2018年12月15日23分56秒

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

2018年12月16日23分56秒

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.

2018年12月16日23分56秒

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.

2018年12月15日23分56秒

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?

2018年12月15日23分56秒

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

2018年12月16日23分56秒

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.

2018年12月16日23分56秒

Sam Well, it's up to you to decide what is more important: your design or the ability for users with vision problems to read your text.

2018年12月15日23分56秒

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).

2018年12月16日23分56秒

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 ?

2018年12月15日23分56秒

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.

2018年12月16日23分56秒

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

2018年12月16日23分56秒

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.

2018年12月15日23分56秒

But I can't pass dp value to setX or setY function, android doesn't translate it here. What can I do?

2018年12月15日23分56秒

It should be ppi instead of dpi

2018年12月16日23分56秒

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.

2018年12月16日23分56秒

getDimensionPixelOffset rounds down, getDimensionPixelSize rounds up.

2018年12月16日23分56秒

I see that this is a late answer to a big question. May I ask what this answer provides that none of the other answers already do?

2018年12月16日23分56秒

Mysticial It gives the answerer 790 points (as of this comment)!

2018年12月16日23分56秒

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."

2018年12月16日23分56秒

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.

2018年12月15日23分56秒

it is not a square!... It's a bar!!!

2018年12月15日23分56秒

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.

2018年12月15日23分56秒

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

2018年12月16日23分56秒

"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

2018年12月16日23分56秒

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)."

2018年12月16日23分56秒

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.

2018年12月15日23分56秒

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

2018年12月15日23分56秒

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

2018年12月16日23分56秒

Your answer is not only incomplete but also was answered way before you even saw the question.

2018年12月16日23分56秒

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

2018年12月15日23分56秒

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

2018年12月15日23分56秒

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

2018年12月16日23分56秒

RoyiNamir Thank you for noticing and update. :)

2018年12月15日23分56秒