This Function convert the DP to Pixel. - Android Graphics

Android examples for Graphics:Pixel

Description

This Function convert the DP to Pixel.

Demo Code


//package com.java2s;
import android.app.Activity;
import android.content.Context;

import android.util.DisplayMetrics;

public class Main {
    /**//from w w  w.j av  a2s  .  c om
     * This Function convert the DP to Pixel.
     * 
     * See below for @params.
     */

    public static int convertDPtoPX(Context ctx, int dp) {

        DisplayMetrics metrics = new DisplayMetrics();
        ((Activity) ctx).getWindowManager().getDefaultDisplay()
                .getMetrics(metrics);
        float logicalDensity = metrics.density;
        int px = (int) (dp * logicalDensity + 0.5);
        System.out.println("DP to Pixel-->" + dp + " = " + px);
        return px;
    }
}

Related Tutorials