px To Dp - Android Graphics

Android examples for Graphics:Pixel

Description

px To Dp

Demo Code


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

public class Main {
    public static int pxToDp(Context context, int px) {
        DisplayMetrics displayMetrics = context.getResources()
                .getDisplayMetrics();//from www  .ja  v a 2 s .c o m
        int dp = Math.round(px
                / (displayMetrics.xdpi / DisplayMetrics.DENSITY_DEFAULT));
        return dp;
    }
}

Related Tutorials