Android Pixel to Dip Convert pxToDp(Context context, int pxValue)

Here you can find the source of pxToDp(Context context, int pxValue)

Description

Convert px value to dp value

Parameter

Parameter Description
context The context of operation
pxValue The px value to be converted to dp

Return

The dp value

Declaration

public static int pxToDp(Context context, int pxValue) 

Method Source Code

//package com.java2s;
import android.content.Context;
import android.util.DisplayMetrics;

public class Main {
    /**//w  w w .ja  v  a  2  s . co m
     * Convert px value to dp value
     * @param context The context of operation
     * @param pxValue The px value to be converted to dp
     * @return The dp value
     */
    public static int pxToDp(Context context, int pxValue) {
        DisplayMetrics displayMetrics = context.getResources()
                .getDisplayMetrics();
        return (int) ((pxValue / displayMetrics.density) + 0.5);
    }
}

Related

  1. px2dip(float pxValue, float scale)
  2. px2dp(Context context, int px)
  3. px2sp(Context context, float pxValue)
  4. px2sp(Context context, float pxValue)
  5. px2sp(float pxValue, float fontScale)
  6. pxToDp(Context ctx, int px)
  7. pxToSp(Context context, int pxValue)
  8. getDpFromPixel(float px, Context context)
  9. convertPxtoDp(Context context, int px)