Android examples for Graphics:Pixel
Converts device specific pixels to density independent pixels.
//package com.java2s; import android.content.Context; import android.content.res.Resources; import android.support.annotation.NonNull; import android.util.DisplayMetrics; public class Main { /**//from w w w . ja v a 2 s. c o m * Converts device specific pixels to density independent pixels. * * @param context Context to get resources and device specific display metrics * @param px A value in px (pixels) unit. Which we need to convert into dp * @return A float value to represent dp equivalent to px value */ public static float convertPixelsToDp(@NonNull Context context, float px) { Resources resources = context.getResources(); DisplayMetrics metrics = resources.getDisplayMetrics(); return px / metrics.density; } }