Android examples for Graphics:Pixel
Converts device specific pixels to dp.
//package com.java2s; import android.content.Context; import android.content.res.Resources; import android.util.DisplayMetrics; public class Main { /**/* www. j av a2s .c om*/ * Converts device specific pixels to dp. * * @param pixels A value in px to be converted to dp * @param context Context to get resources and device specific display metrics * @return A float value to represent dp equivalent to px value */ public static float convertPixelsToDp(float pixels, Context context) { Resources resources = context.getResources(); DisplayMetrics metrics = resources.getDisplayMetrics(); return pixels / ((float) metrics.densityDpi / DisplayMetrics.DENSITY_DEFAULT); } }