Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Apache License 

import android.content.Context;

import android.util.DisplayMetrics;

import android.view.WindowManager;

public class Main {
    /**
     * Get the current device physical display density.
     *
     * @param context valid context.
     * @return <b>int</b> with DENSITY_LOW, DENSITY_MEDIUM, DENSITY_TV, DENSITY_HIGH, DENSITY_XHIGH,
     * DENSITY_XXHIGH
     */
    public static int getDisplayDensity(final Context context) {
        // return (double)DisplayMetrics.DENSITY_DEFAULT * getDisplayMetrics(context).density;
        return getDisplayMetrics(context).densityDpi;
    }

    /**
     * Returns a valid DisplayMetrics object
     *
     * @param context valid context
     * @return DisplayMetrics object
     */
    public static DisplayMetrics getDisplayMetrics(final Context context) {
        final WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        final DisplayMetrics metrics = new DisplayMetrics();
        windowManager.getDefaultDisplay().getMetrics(metrics);
        return metrics;
    }
}