scale-independent pixel - android

I don't know how it convert to the unit of pixels. For example, if I assign 10sp to my font size on 160 dpi device, how much px of my font size is it ? Or, if I assign 10sp to my font on 240 dpi device, how much px of my font size is it ?

They're just the same as dip, but they also take into account the font scaling factor that the user sets on his device. So if he/she left font size set to "normal", it's just like dip. If there is a font scaling factor, you can get the whole scaled density from DisplayMetrics.scaledDensity.
(I have to admit I just tried to look for some "font size" option in my Android phone global settings and I couldn't find it, so I wonder if it is of any use right now. Am I missing something?)

This depends on screen density set for the device. You can obtain it with
screenDensity = this.getResources().getDisplayMetrics().density;
Then by multiplying screenDensity and the size in dp or sp you can get actual pixel size. For example on 320x240 device the creen density is about 0.75, while on HTC Desire HD and Galaxy Tab is 1.5 even though they have different resolution and may not have the same dpi.

Related

How to convert font size value in pt to sp [duplicate]

I am a visual designer, working on an android design and I"m trying to spec my PSD file for our engineers. I cannot seem to find any documentation regarding the conversion of point size to SP for the type in any Android documentation. (Just that SP should be used for type).
The problem is that it depends on the density. Really, they're not good measurements to try to compare.
Points: There are 12 points in a pica, 6 picas per inch, so 72 (more accurately 72.27) points per inch.
Device-Independent Pixels (DP): These will be equal to the pixel size for MDPI displays, 1.5x the pixel size for HDPI displays, and 2x the pixel size for XHDPI displays. (e.g. 12dp = 12px MDPI, 18px HDPI, 24px XHDPI).
Scaled Pixels: These will be equivalent to the DP value, but scaled according to the user preference to be smaller or larger.
If you're designing at 72 dpi (Photoshop default DPI setting), an 8pt font would be equivalent to 8px, which would be 8px at MDPI, 12px at HDPI, and 16px at XHDPI, or more simply 8dp for all densities. I don't know how much scaling gets applied to different SP settings, so you'd have to hunt that out, but basically I would just give them the mockup and let them size it appropriately from there -- surely they can get it pretty close visually from that.
In contrast to dp (and sp with minus the user text sizing factor), pt has pretty much the exact same physical measurement. Dp is more or less a rough approximation, since it uses the same factor for a wide dpi range.
You cannot convert pt to sp, like said by Flavio. You can, however convert pt to dp roughly (+/- 30% in extreme cases, but at least expect +/-10%).
Using the dpi buckets (ldp=120,mdpi=160,hdpi=240,xhdpi=320) the most generic conversion factor would be x0.45 (= 72/120*0.75 = 72/160*1 = 72/240*1,5 = 72/320*2) if using the exact dpi bucket values.
For more precise factors:
mdpi (scaling factor: 1.0) 10.1" WXGA Screen (149dpi) is 0.483076923 (= 72/149 * 1.0)
hdpi (scaling factor: 1.5) Nexus 7 (216dpi) is 0.5 (= 72/216 * 1.5)
xhdpi (scaling factor: 2.0) Nexus 10 (300dpi) is 0.48 (= 72/300 * 2)
So 12 pt would be on...
10.1" WXGA: 24.84dp
Nexus 7: 24 dp
Nexus 10: 25dp
dpi bucket value: 26.666dp
You could use the same values for sp, but keep in mind, that these could be in/decreased by the users settings.
There's no direct mapping between pixels and sp, since sps are dynamically set as the users change the device font size in system settings. The user may want to see all the app fonts in smaller or larger sizes. It's an accessibility feature provided by the OS.
^What Flavio said. but check out the display metrics class overview on the Android Developers site. You might find what you need here... http://developer.android.com/reference/android/util/DisplayMetrics.html#
This is a great tool to help convert between the various measurements - put in what you know and it'll calculate the rest!
http://angrytools.com/android/pixelcalc/
There are 72 pts per inch, and 160 sp per inch (with standard font size in the device settings). this means there are 160sp per 72pts. 160sp/72pts simplifies to 20/9. So when converting pts to sp or dp use pts*20/9. As mentioned by others here already, sp does vary by the user's settings so if you don't want the text size to change with the device settings, then use dp.

Android XML 1dp is not equal 1px

I try to make XML layout from PSD file. Right now PSD file have dimensions 320x1440. I have read on android.developers that mdpi (160dpi) have resolution 320x480. So I is assumed that in that case 1px in my PSD file is equal to 1dp. Thats the theory.
But I spotted little differences - especially in font sizes. For example I in the project some font have 24px size I need to divide it by 2 (like 320dpi but PSD file is 320px). Also if I have element which is 300px height in my PSD file it is not 300dp in android studio.
I have tried to google answer but had no luck. Any help is much appreciated.
From this answer:
px is one pixel.
sp is scale-independent pixels.
dip is Density-independent pixels.
You would use
sp for font sizes
dip for everything else.
dip==dp
From Android Developers center:
px
Pixels - corresponds to actual pixels on the screen.
in
Inches - based on the physical size of the screen.
1 Inch = 2.54 centimeters
mm
Millimeters - based on the physical size of the screen.
pt (it depends upon physical size of the screen)
Points - 1/72 of an inch based on the physical size of the screen.
This will help you for more understanding.
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.

Confusion about Resolution, Screen aspects in Android

I'm asking this question after much reading. I've always heard that dpi is for printers, but when it comes to screen now, they are also talking about dpi. Are they referring to ppi?
Now, what is really resolution, for me its the the number of pixels each dimension can display, e.g. 800x600 means 800 pixesl on width and 600 pixels on the height, but at some places I'm seeing that they are referring to resolution as dpi.
I'm trying to understand this concept well because its very important in Android, like in this article,
For example, say an icon is intended to be 0.5x0.5 in when rendered on a screen. Next, the image must be created at the largest density supported, or as a scalable vector graphic. Best practice is to support the maximum density, which currently is xxhdpi at 480 dpi. At 480 dpi, a 0.5x0.5 in image converts to 240x240 px.
So it is referring dpi as ppi actually if I understand?
So far what I've understood is that different pixels may render different number of pixels. This is why we don't use pixels as measurement unit. Instead we use dp, where a dp is one pixel on 160 dpi device (again the confusion about dpi & ppi)
Can someone clear this big confusion or direct me to an article that may clear it
Mate, Resolution being 800 X 600 implies that the screen has 480,000 pixel points that will be used to render the screen(This is often confused with the dimensions of the display). DPI or PPI means dots/points per inch, this is the measure of the density of the screen.
So just given the Resolution, one can not determine the actual length of the display unless the density parameter is also available. So a 800 X 600 resolution has 480,000 Pixel points & a let this device has a density of 480 dpi.
So the Width of the screen
= No of pixel points along its width/Density
= 800/480
= 1.67 inches
Similarly,
Height = 600/480
=1.25 inches
and if 800X600 resolution device has density of 160 dpi, its dimensions will vary drastically. Following calculations calculate Height/Width of 800X600 on 160 dpi. Compare these two values with above 480 dpi calculations.
the Width of the screen
= No of pixel points along its width/Density
= 800/160
= 5.0 inches
Similarly,
Height = 600/160
=3.75 inches
This is the very reason that scaling images to best fit the screen is such a complex issue on frafmented android environment.However, I love android!
Hope this helps!
and any one who has some thing to add/delete.modify to this answer is most welcome.
dpi (dots per inch) == ppi (pixels per inch)
You are also talking about the DisplayMetrics.density, which gives you the multiplier for the dp unit of measurement.
There's also DisplayMetrics.scaledDensity which also takes into account text size user chose.
To put it plainly, dp unit is intended to give you some security about size of your objects on screen. 160dp should represent one inch on any screen. In order to achieve that, you have to multiply your dimension by DisplayMetrics.density or DisplayMetrics.scaledDensity.
That is, if you're doing that in code. For Layouts, you can just enter a View's dimensions in dp and have Android framework take care of that for you.

How do I convert pt to sp?

I am a visual designer, working on an android design and I"m trying to spec my PSD file for our engineers. I cannot seem to find any documentation regarding the conversion of point size to SP for the type in any Android documentation. (Just that SP should be used for type).
The problem is that it depends on the density. Really, they're not good measurements to try to compare.
Points: There are 12 points in a pica, 6 picas per inch, so 72 (more accurately 72.27) points per inch.
Device-Independent Pixels (DP): These will be equal to the pixel size for MDPI displays, 1.5x the pixel size for HDPI displays, and 2x the pixel size for XHDPI displays. (e.g. 12dp = 12px MDPI, 18px HDPI, 24px XHDPI).
Scaled Pixels: These will be equivalent to the DP value, but scaled according to the user preference to be smaller or larger.
If you're designing at 72 dpi (Photoshop default DPI setting), an 8pt font would be equivalent to 8px, which would be 8px at MDPI, 12px at HDPI, and 16px at XHDPI, or more simply 8dp for all densities. I don't know how much scaling gets applied to different SP settings, so you'd have to hunt that out, but basically I would just give them the mockup and let them size it appropriately from there -- surely they can get it pretty close visually from that.
In contrast to dp (and sp with minus the user text sizing factor), pt has pretty much the exact same physical measurement. Dp is more or less a rough approximation, since it uses the same factor for a wide dpi range.
You cannot convert pt to sp, like said by Flavio. You can, however convert pt to dp roughly (+/- 30% in extreme cases, but at least expect +/-10%).
Using the dpi buckets (ldp=120,mdpi=160,hdpi=240,xhdpi=320) the most generic conversion factor would be x0.45 (= 72/120*0.75 = 72/160*1 = 72/240*1,5 = 72/320*2) if using the exact dpi bucket values.
For more precise factors:
mdpi (scaling factor: 1.0) 10.1" WXGA Screen (149dpi) is 0.483076923 (= 72/149 * 1.0)
hdpi (scaling factor: 1.5) Nexus 7 (216dpi) is 0.5 (= 72/216 * 1.5)
xhdpi (scaling factor: 2.0) Nexus 10 (300dpi) is 0.48 (= 72/300 * 2)
So 12 pt would be on...
10.1" WXGA: 24.84dp
Nexus 7: 24 dp
Nexus 10: 25dp
dpi bucket value: 26.666dp
You could use the same values for sp, but keep in mind, that these could be in/decreased by the users settings.
There's no direct mapping between pixels and sp, since sps are dynamically set as the users change the device font size in system settings. The user may want to see all the app fonts in smaller or larger sizes. It's an accessibility feature provided by the OS.
^What Flavio said. but check out the display metrics class overview on the Android Developers site. You might find what you need here... http://developer.android.com/reference/android/util/DisplayMetrics.html#
This is a great tool to help convert between the various measurements - put in what you know and it'll calculate the rest!
http://angrytools.com/android/pixelcalc/
There are 72 pts per inch, and 160 sp per inch (with standard font size in the device settings). this means there are 160sp per 72pts. 160sp/72pts simplifies to 20/9. So when converting pts to sp or dp use pts*20/9. As mentioned by others here already, sp does vary by the user's settings so if you don't want the text size to change with the device settings, then use dp.

Minimum size of background image in Android app

I want to keep an image in the res/drawable folder, which would be used as the background in my app. I wish that my app should work across all devices of varying densities. What minimum size in pixels should the image be kept to maintain balance between a reasonable app size and also supporting multiple screens.
The developer guide mentions that:
xlarge screens are at least 960dp x 720dp
large screens are at least 640dp x 480dp
normal screens are at least 470dp x 320dp
small screens are at least 426dp x 320dp
I use Photoshop where the images are always in pixels and not dp and hence the confusion. By the way, I am quite new at Android as well as Photoshop.
If you want to convert dp values to pixels u can use following formula:
int px = (int) (dp * logicalDensity + 0.5);
To get dp from pixels, divide the density into the pixel value rather than multiply.
Try to study about 9-patch image. This single image could solve your issue.
You have to create images 320x480 size save in drawable-mdpi folder and 480x800 size save in drawable-hdpi folder
These both size image run in every small and large screen device.
for medium device 320x480 and for high density 480x800.
Don't use a fixed size image at all. Use a little image as a tiled background or define background as a gradient or whatever using xml resources.
px is one pixel. scale-independent pixels ( sp ) and density-independent pixels ( dip ) you want to use sp for font sizes and dip for everything else. dip==dp from here http://developer.android.com/guide/topics/resources/more-resources.html#Dimension
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.

Categories

Resources