Example usage for android.view Display getRealMetrics

List of usage examples for android.view Display getRealMetrics

Introduction

In this page you can find the example usage for android.view Display getRealMetrics.

Prototype

public void getRealMetrics(DisplayMetrics outMetrics) 

Source Link

Document

Gets display metrics based on the real size of this display.

Usage

From source file:com.skytree.epubtest.BookViewActivity.java

@SuppressLint("NewApi")
public int getRawHeight() {
    int width = 0, height = 0;
    final DisplayMetrics metrics = new DisplayMetrics();
    Display display = getWindowManager().getDefaultDisplay();
    Method mGetRawH = null, mGetRawW = null;

    try {/*from   w  w  w.j a  va  2 s.  c  o  m*/
        // For JellyBeans and onward
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN) {
            display.getRealMetrics(metrics);
            width = metrics.widthPixels;
            height = metrics.heightPixels;
        } else {
            mGetRawH = Display.class.getMethod("getRawHeight");
            mGetRawW = Display.class.getMethod("getRawWidth");
            try {
                width = (Integer) mGetRawW.invoke(display);
                height = (Integer) mGetRawH.invoke(display);
            } catch (IllegalArgumentException e) {
                e.printStackTrace();
                return 0;
            } catch (IllegalAccessException e) {
                e.printStackTrace();
                return 0;
            } catch (InvocationTargetException e) {
                e.printStackTrace();
                return 0;
            }
        }
        return height;
    } catch (NoSuchMethodException e3) {
        e3.printStackTrace();
        return 0;
    }
}

From source file:com.skytree.epubtest.BookViewActivity.java

@SuppressLint("NewApi")
public int getRawWidth() {
    int width = 0, height = 0;
    final DisplayMetrics metrics = new DisplayMetrics();
    Display display = getWindowManager().getDefaultDisplay();
    Method mGetRawH = null, mGetRawW = null;

    try {// ww  w. j a va 2  s .c o  m
        // For JellyBeans and onward
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN) {
            display.getRealMetrics(metrics);

            width = metrics.widthPixels;
            height = metrics.heightPixels;
        } else {
            mGetRawH = Display.class.getMethod("getRawHeight");
            mGetRawW = Display.class.getMethod("getRawWidth");

            try {
                width = (Integer) mGetRawW.invoke(display);
                height = (Integer) mGetRawH.invoke(display);
            } catch (IllegalArgumentException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                return 0;
            } catch (IllegalAccessException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                return 0;
            } catch (InvocationTargetException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                return 0;
            }
        }
        return width;
    } catch (NoSuchMethodException e3) {
        e3.printStackTrace();
        return 0;
    }
}