Convert Dip to Px in Android

How to convert dp to pixels and and use it to draw to canvas in android

I think there's either a typo in your question or you really don't load the value for the height - you are actually loading a number representing the id of the resource. It should have been:

int buttonWidth = (int) dpToPixel(getResorces().getDimension(R.dimen.buttonHeight));

But...you don't need to do the transformation yourself:

getResources().getDimensionPixelSize(R.dimen.buttonHeight);

Convert dip to px in Android

The formula is: px = dp * (dpi / 160), for having on a 160 dpi screen. See Convert dp units to pixel units for more information.

You could try:

public static int convertDipToPixels(float dips) {
return (int) (dips * appContext.getResources().getDisplayMetrics().density + 0.5f);
}

Hope this helps...

How to convert DP, PX, SP among each other, especially DP and SP?

DP to PX:

public static int dpToPx(float dp, Context context) {
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, context.getResources().getDisplayMetrics());
}

SP to PX:

public static int spToPx(float sp, Context context) {
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, sp, context.getResources().getDisplayMetrics());
}

DP to SP:

public static int dpToSp(float dp, Context context) {
return (int) (dpToPx(dp, context) / context.getResources().getDisplayMetrics().scaledDensity);
}

Converting dp to px without Context

is there some case where it might fail?

Yes, there is!

Android supports different screens, for example you might cast the app with Chromecast or connect to a different screen by other means. In that case the values will not be converted properly to that other screen.

From the documentation for Resources.getSystem():

Return a global shared Resources object that provides access to only
system resources (no application resources), and is not configured for
the current screen (can not use dimension units, does not change based
on orientation, etc).

Formula px to dp, dp to px android

Note: The widely used solution above is based on displayMetrics.density. However, the docs explain that this value is a rounded value, used with the screen 'buckets'. Eg. on my Nexus 10 it returns 2, where the real value would be 298dpi (real) / 160dpi (default) = 1.8625.

Depending on your requirements, you might need the exact transformation, which can be achieved like this:

[Edit] This is not meant to be mixed with Android's internal dp unit, as this is of course still based on the screen buckets. Use this where you want a unit that should render the same real size on different devices.

Convert dp to pixel:

public int dpToPx(int dp) {
DisplayMetrics displayMetrics = getContext().getResources().getDisplayMetrics();
return Math.round(dp * (displayMetrics.xdpi / DisplayMetrics.DENSITY_DEFAULT));
}

Convert pixel to dp:

public int pxToDp(int px) {
DisplayMetrics displayMetrics = getContext().getResources().getDisplayMetrics();
return Math.round(px / (displayMetrics.xdpi / DisplayMetrics.DENSITY_DEFAULT));
}

Note that there are xdpi and ydpi properties, you might want to distinguish, but I can't imagine a sane display where these values differ greatly.

Converting from dp to px in android

Please go through this post, i think you will get your answer :)

What is the difference between "px", "dp", "dip" and "sp" on Android?

How to convert dp to px in xamarin.android?

Solution to "First code":

Xamarin tends to move constants into their own enums. COMPLEX_UNIT_DIP can be found on ComplexUnitType enum. Also you cannot have < HEIGHT > in your code you actually need to pass in dips to get the equivalent pixel value. in the example below I am getting pixels in 100 dips.

var dp = 100;
int pixel = (int)TypedValue.ApplyDimension(ComplexUnitType.Dip, dp, Context.Resources.DisplayMetrics);

Solution to "Second code":

You need to explicitly cast 'DisplayMetrics.DensityDefault' to a float and the entire round to an int:

int pixel = (int)System.Math.Round(dp * (displayMetrics.Xdpi / (float)DisplayMetrics.DensityDefault));

I prefer the first approach as the second code is specifically for calculating along the "x dimension":
Per Android docs and Xamarin.Android docs, the Xdpi property is

"The exact physical pixels per inch of the screen in the X dimension."

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.

conversion from dp to pixel wrong

Use this

int pixels = getResources().getDimensionPixelSize(R.dimen.button_left_right_margin);


Related Topics



Leave a reply



Submit