get Screen Matrices via reflection - Android User Interface

Android examples for User Interface:Screen Resolution

Description

get Screen Matrices via reflection

Demo Code


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

import android.util.DisplayMetrics;
import android.view.Display;
import java.lang.reflect.Method;

public class Main {

    public static int[] getRealMetrics(Activity activity) {
        int[] dpi = new int[2];
        Display display = activity.getWindowManager().getDefaultDisplay();
        DisplayMetrics dm = new DisplayMetrics();
        @SuppressWarnings("rawtypes")
        Class c;/* w  ww  .  j  a va 2  s.  co m*/
        try {
            c = Class.forName("android.view.Display");
            @SuppressWarnings("unchecked")
            Method method = c.getMethod("getRealMetrics",
                    DisplayMetrics.class);
            method.invoke(display, dm);
            dpi[0] = dm.widthPixels;
            dpi[1] = dm.heightPixels;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return dpi;
    }
}

Related Tutorials