Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import android.app.Activity;

import android.content.Context;

import android.content.res.Resources;

import android.util.DisplayMetrics;

import android.view.Display;

public class Main {
    public static float convertDpToPixel(final float dp, final Context context) {
        final Resources resources = context.getResources();
        final DisplayMetrics metrics = resources.getDisplayMetrics();
        final float px = dp * (metrics.densityDpi / 160f);
        return px;
    }

    private static DisplayMetrics getDisplayMetrics(final Activity activity) {
        final DisplayMetrics metrics = new DisplayMetrics();
        final Display display = activity.getWindowManager().getDefaultDisplay();
        display.getMetrics(metrics);
        return metrics;
    }
}