Example usage for android.util DisplayMetrics DisplayMetrics

List of usage examples for android.util DisplayMetrics DisplayMetrics

Introduction

In this page you can find the example usage for android.util DisplayMetrics DisplayMetrics.

Prototype

public DisplayMetrics() 

Source Link

Usage

From source file:Main.java

public static int getScreenWidth(Context context) {
    WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    Display display = windowManager.getDefaultDisplay();
    DisplayMetrics metrics = new DisplayMetrics();
    display.getMetrics(metrics);//from   w  w  w  .ja  v  a  2 s  . c om
    return metrics.widthPixels;
}

From source file:Main.java

public static int getHeight(Activity activity) {
    if (mScreenHeight == 0) {
        DisplayMetrics dm = new DisplayMetrics();
        activity.getWindowManager().getDefaultDisplay().getMetrics(dm);
        mScreenHeight = dm.heightPixels;
    }//from w w  w.  j  a  v  a 2 s  . c o m
    return mScreenHeight - getActionBarHeight(activity);
}

From source file:Main.java

public static DisplayMetrics getDeviceDisplayMetrics(Context context) {
    android.view.WindowManager windowsManager = (android.view.WindowManager) context
            .getSystemService(Context.WINDOW_SERVICE);
    android.view.Display display = windowsManager.getDefaultDisplay();
    DisplayMetrics outMetrics = new DisplayMetrics();
    display.getMetrics(outMetrics);//from   ww  w  .j av a 2s.c o  m

    return outMetrics;
}

From source file:Main.java

public static Bitmap captureWithStatusBar(Activity activity) {
    View view = activity.getWindow().getDecorView();
    view.setDrawingCacheEnabled(true);//from   w  w w .  j ava 2s .com
    view.buildDrawingCache();
    Bitmap bmp = view.getDrawingCache();
    DisplayMetrics dm = new DisplayMetrics();
    activity.getWindowManager().getDefaultDisplay().getMetrics(dm);
    Bitmap ret = Bitmap.createBitmap(bmp, 0, 0, dm.widthPixels, dm.heightPixels);
    view.destroyDrawingCache();
    return ret;
}

From source file:Main.java

public static int dp2px(Context context, int dp) {
    WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    Display display = wm.getDefaultDisplay();

    DisplayMetrics displaymetrics = new DisplayMetrics();
    display.getMetrics(displaymetrics);//from  w  ww. j av  a  2 s  .c om

    return (int) (dp * displaymetrics.density + 0.5f);
}

From source file:Main.java

public static DisplayMetrics getDisplayMetrics(Context context) {
    if (metrics == null) {
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        metrics = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(metrics);
    }/*from ww w. ja  va 2 s. c  o  m*/

    return metrics;
}

From source file:Main.java

public static void initFillWidthAndHeight(Activity activity) {
    DisplayMetrics dm = new DisplayMetrics();
    activity.getWindowManager().getDefaultDisplay().getMetrics(dm);
    fillWidth = dm.widthPixels;/*w  w  w.  j  a  v  a 2 s  . c o  m*/
    fillHeight = dm.heightPixels;
}

From source file:Main.java

public static Point getSize(Display display) {
    if (Build.VERSION.SDK_INT >= 17) {
        Point outPoint = new Point();
        DisplayMetrics metrics = new DisplayMetrics();
        display.getRealMetrics(metrics);
        outPoint.x = metrics.widthPixels;
        outPoint.y = metrics.heightPixels;
        return outPoint;
    }//from   w w  w.  j a v a 2s .  com
    if (Build.VERSION.SDK_INT >= 14) {
        Point outPoint = getRealSize(display);
        if (outPoint != null)
            return outPoint;
    }
    Point outPoint = new Point();
    if (Build.VERSION.SDK_INT >= 13) {
        display.getSize(outPoint);
    } else {
        outPoint.x = display.getWidth();
        outPoint.y = display.getHeight();
    }
    return outPoint;
}

From source file:Main.java

public static int getDpi(Activity activity) {
    WindowManager wm = activity.getWindowManager();
    //wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
    Display dp = wm.getDefaultDisplay();
    DisplayMetrics dm = new DisplayMetrics();
    dp.getMetrics(dm);//from w w  w. j a va  2  s .  c  om
    return dm.densityDpi;
}

From source file:Main.java

/**
 * This method to get screen size./* ww w.j a v  a2s .c o m*/
 * 
 * @param context
 * @return window manager instance which contains width and height of the
 *         screen
 */
public static DisplayMetrics getScreenSize(Context context) {
    // TODO Auto-generated method stub
    DisplayMetrics dm = new DisplayMetrics();
    WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    wm.getDefaultDisplay().getMetrics(dm);
    return dm;
}