Conversion from pixels to DIPs (Density-independent pixels) - Android Graphics

Android examples for Graphics:Pixel

Description

Conversion from pixels to DIPs (Density-independent pixels)

Demo Code


//package com.java2s;

import android.content.Context;
import android.content.res.Resources;

import android.util.TypedValue;

public class Main {
    /**/*from ww  w  .  ja v a2  s  .  c o m*/
     * Conversion from pixels to DIPs (Density-independent pixels)
     * @param context
     * @param pixels
     * @return
     */
    public static int convertPixelsToDips(Context context, int pixels) {
        Resources resources = context.getResources();
        float dipValue = TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_DIP, pixels,
                resources.getDisplayMetrics());
        resources = null;
        return (int) dipValue;
    }
}

Related Tutorials