Does setWidth(int pixels) use dip or px?
It uses pixels, but I'm sure you're wondering how to use dips instead. The answer is in TypedValue.applyDimension()
. Here's an example of how to convert dips to px in code:
// Converts 14 dip into its equivalent px
Resources r = getResources();
int px = Math.round(TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP, 14,r.getDisplayMetrics()));
What is the difference between px, dip, dp, and sp?
From the Android Developer Documentation:
px
Pixels - corresponds to actual pixels on the screen.in
Inches - based on the physical size of the screen.
1 Inch OR 2.54 centimetersmm
> 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 or dip
> 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
> Scaleable Pixels OR scale-independent pixels - this is like the dp unit, but it is also scaled by the user's font size preference. It is recommended you
use this unit when specifying font sizes, so they will be adjusted
for both the screen density and the user's preference. Note, the Android documentation is inconsistent on whatsp
actually stands for, one doc says "scale-independent pixels", the other says "scaleable pixels".
From Understanding Density Independence In Android:
Density Bucket | Screen Density | Physical Size | Pixel Size |
---|---|---|---|
ldpi | 120 dpi | 0.5 x 0.5 in | 0.5 in * 120 dpi = 60x60 px |
mdpi | 160 dpi | 0.5 x 0.5 in | 0.5 in * 160 dpi = 80x80 px |
hdpi | 240 dpi | 0.5 x 0.5 in | 0.5 in * 240 dpi = 120x120 px |
xhdpi | 320 dpi | 0.5 x 0.5 in | 0.5 in * 320 dpi = 160x160 px |
xxhdpi | 480 dpi | 0.5 x 0.5 in | 0.5 in * 480 dpi = 240x240 px |
xxxhdpi | 640 dpi | 0.5 x 0.5 in | 0.5 in * 640 dpi = 320x320 px |
Converting dip to pixels yielding the same value
Have you tried
px = dp * (dpi / 160)
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.
Please check this link for more details.
Give padding with setPadding with dip unit not px unit
Use following code to get pixels from given value in dp
.
Resources res = getResources();
float value = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, valueInDP, res.getDisplayMetrics());
where valueInDP is value in dp and this will return corresponding pixel value according to screen density.
or you can use following -
float value = valueInDP * getResources().getDisplayMetrics().density;
Android and setting width and height programmatically in dp units
You'll have to convert it from dps to pixels using the display scale factor.
final float scale = getContext().getResources().getDisplayMetrics().density;
int pixels = (int) (dps * scale + 0.5f);
Access to hardware pixels on mobile devices
Screen scale factor (Retina, presently either 2x or 3x) is distinct from page scale factor. At the same page scale factor, 1px would be 1x1, 2x2, or 3x3 hardware pixels, depending on the (Retina) display.
It sounds like what you're looking to do is to drive the screen at a "native" resolution which would make it appear that it has 2x or 3x as many pixels, but at a (non-Retina) screen scale of 1.
To accomplish that, you'd have to transform the view by multiplying its size by the screen scale factor, while scaling it by the inverse of its screen factor.
You can set the head's <meta name="viewport" content="width=device-width-times-2, initial-scale=0.5">
but your content will be far less readable and sharp.
If you're looking to do this on an element basis, you can set
-webkit-transform-style: preserve-3d;
-webkit-transform: scale3d(0.5,0.5,0.5);
3d is necessary, as a 2d transformation may lead to issues with the touch area not matching up with the element's location in the view.
Should I use dip, dp, sip or sp for text formatting?
If you just want to make some text bigger or smaller you should prefer using default styles provided by Android:
<TextView ... android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView ... android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView ... android:textAppearance="?android:attr/textAppearanceSmall" />
Neither of them is default, default is:
<TextView ... android:textAppearance="?android:attr/textAppearance" />
It can be surely applied not only to TextView
, but to other views as well.
Margins of a LinearLayout, programmatically with dp
You can use DisplayMetrics and determine the screen density. Something like this:
int dpValue = 5; // margin in dips
float d = context.getResources().getDisplayMetrics().density;
int margin = (int)(dpValue * d); // margin in pixels
As I remember it's better to use flooring for offsets and rounding for widths.
Is `canvas.drawCircle` metho's radius in dp units?
In android, the code for drawing a circle using Canvas
the float
argument is in pixels
.
you say float
, is the float
a pixel representation.
Below is the sample code for converting dp to px and px to dp.
To convert dp to pixel.
public static int dp2px(Resources resource, int dp) {
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp,resource.getDisplayMetrics());
}
To convert pixel to dp.
public static float px2dp(Resources resource, float px) {
return (float) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_PX, px,resource.getDisplayMetrics());
}
Conversion Code is from here.
Related Topics
How to Set Spinner Default Value to Null
How to Show Progress Dialog in Android
How to Execute a Method by Clicking a Notification
Inserting Contacts in Android 2.2
Anr Keydispatchingtimedout Error
Receive Mms Messages in Android Kitkat
Broadcast Receiver to Detect Application Start
Why Is the Android Test Runner Reporting "Empty Test Suite"
What Are the Differences Among Internal Storage, External Storage, Sd Card and Removable Storage
How to Use Startactivityforresult() for Email Intent
How to Show Dialog in Oncreate Method
How to Verify If User Has Network Access and Show a Pop-Up Alert When There Isn'T
Occured an Internal_Error When Requestemail from Googlesigninoptions Android