How to Calculate Dp from Pixels in Android Programmatically

How to calculate dp from pixels in android programmatically

All the answers here show a dp->px conversion rather than px->dp, which is what the OP asked for.
Note that TypedValue.applyDimension cannot be used to convert px->dp, for that you must use the method described here: https://stackoverflow.com/a/17880012/504611 (quoted below for convenience).

fun Context.dpToPx(dp: Int): Int {
return (dp * resources.displayMetrics.density).toInt()
}

fun Context.pxToDp(px: Int): Int {
return (px / resources.displayMetrics.density).toInt()
}

How to convert px to dp in margins programmatically?

Multiply your pixel values by the device display density.

final float density = context.getResources().getDisplayMetrics().density;
final int valueInDp = (int)(valueInPixels * density);

In your case would be as next:

ArrowUp.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

final float density = view.getResources().getDisplayMetrics().density;

if(!Switch){
setMargins(AdditionalOperations, 0, 0, 0, 800 * density);
Switch = true;
}
else{
setMargins(AdditionalOperations, 0, 0, 0, 715 * density);
Switch = false;
}
}
});

Tip: As you are using constant values, you may want to cache them instead of computing the values all the time.

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.

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);
}

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

How to calculate the pixel density (dp) of a device?

Here you can find how to calculate sw???dp for every device you want.

Application Skeleton to support multiple screen

I think that instead of 96 x 5' (screen size) you should use these qualifiers: http://developer.android.com/design/style/devices-displays.html (160, 240, 320, 480). In this case of Galaxy S4, you get the good result, but if you try for example with a Nexus4 (4,7 ') you will get sw~272 instead of 380 as it should, and as happens in practice. This is the value you should use in your formula and you will get the right result.

OR

run this snippet code: (API 13+)

int sw = getResources().getConfiguration().smallestScreenWidthDp;



Related Topics



Leave a reply



Submit