Example usage for android.view WindowManager getDefaultDisplay

List of usage examples for android.view WindowManager getDefaultDisplay

Introduction

In this page you can find the example usage for android.view WindowManager getDefaultDisplay.

Prototype

public Display getDefaultDisplay();

Source Link

Document

Returns the Display upon which this WindowManager instance will create new windows.

Usage

From source file:com.tencent.wetest.common.util.DeviceUtil.java

/**
 * ??//from   w ww. j a v a2  s  .  com
 * @param cx 
 * @return ? ( width x height )
 */

public static String getDisplayMetrics(Context cx) {

    WindowManager mWindowManager = (WindowManager) cx.getSystemService(Context.WINDOW_SERVICE);
    Display display = mWindowManager.getDefaultDisplay();
    DisplayMetrics metric = new DisplayMetrics();

    Point size = new Point();

    String str = "";

    try {

        if (Build.VERSION.SDK_INT >= 11) {

            display.getRealSize(size);

            str += String.valueOf(size.x) + " x " + String.valueOf(size.y);

        } else {

            Method method = Class.forName("android.view.Display").getMethod("getRealMetrics",
                    DisplayMetrics.class);
            method.invoke(display, metric);

            str += String.valueOf(metric.widthPixels) + " x " + String.valueOf(metric.heightPixels);

        }

    } catch (Exception e) {

        display.getMetrics(metric);
        str += String.valueOf(metric.widthPixels) + " x " + String.valueOf(metric.heightPixels);
        Logger.error("getDeviceRealMetric Exception : " + e.toString());

    }

    return str;

}

From source file:com.lanma.customviewproject.utils.ScrollingUtil.java

/**
 * ??,???px//from  www  . ja v a 2s.  c  om
 */
public static int getScreenHeight(Context context) {
    WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    DisplayMetrics dm = new DisplayMetrics();
    windowManager.getDefaultDisplay().getMetrics(dm);
    return dm.heightPixels;
}

From source file:com.amazon.android.utils.Helpers.java

/**
 * Returns the screen/display size.//  w  w  w .j a  v  a  2  s.  co m
 *
 * @param context The context.
 * @return The display size.
 */
public static Point getDisplaySize(Context context) {

    WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    Display display = wm.getDefaultDisplay();
    Point size = new Point();
    display.getSize(size);
    return size;
}

From source file:com.charon.materialsample.view.ScrollingTabs.java

public static int getWindowWidth(Context context) {
    WindowManager manager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    DisplayMetrics outMetrics = new DisplayMetrics();
    manager.getDefaultDisplay().getMetrics(outMetrics);
    return outMetrics.widthPixels;
}

From source file:com.kayac.slidingmenu.ui.views.DraggableLayout.java

public static Point getScreenSize(Context c) {
    Point p = new Point();
    WindowManager wm = (WindowManager) c.getSystemService(Context.WINDOW_SERVICE);
    Display display = wm.getDefaultDisplay();
    p.x = display.getWidth();//  ww w.j a  va  2s. c  o m
    p.y = display.getHeight();
    return p;
}

From source file:FacebookImageLoader.java

public static int calculateDensityDpi(Context context) {
    if (mDensityDpi > 0) {
        // we've already calculated it
        return mDensityDpi;
    }//from  w  ww.ja  v a  2s.  c  o  m

    if (Integer.parseInt(Build.VERSION.SDK) <= 3) {
        // 1.5 devices are all medium density
        mDensityDpi = DENSITY_MEDIUM;
        return mDensityDpi;
    }

    DisplayMetrics metrics = new DisplayMetrics();
    WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    wm.getDefaultDisplay().getMetrics(metrics);
    int reflectedDensityDpi = DENSITY_MEDIUM;

    try {
        reflectedDensityDpi = DisplayMetrics.class.getDeclaredField("mDensityDpi").getInt(metrics);
    } catch (Exception e) {
        e.printStackTrace();
    }

    mDensityDpi = reflectedDensityDpi;
    return mDensityDpi;
}

From source file:com.syncedsynapse.kore2.utils.UIUtils.java

/**
 * Fills the standard cast info list, consisting of a {@link android.widget.GridLayout}
 * with actor images and a Textview with the name and the role of the additional cast.
 * The number of actor presented on the {@link android.widget.GridLayout} is controlled
 * through the global setting, and only actors with images are presented.
 * The rest are presented in the additionalCastView TextView
 *
 * @param context Activity//from w w w.  jav  a  2 s .  co  m
 * @param castList Cast list
 * @param castListView GridLayout on which too show actors that have images
 * @param additionalCastTitleView View with additional cast title
 * @param additionalCastView Additional cast
 */
public static void setupCastInfo(final Context context, List<VideoType.Cast> castList, GridLayout castListView,
        TextView additionalCastTitleView, TextView additionalCastView) {
    HostManager hostManager = HostManager.getInstance(context);
    Resources resources = context.getResources();
    DisplayMetrics displayMetrics = new DisplayMetrics();
    WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    windowManager.getDefaultDisplay().getMetrics(displayMetrics);

    View.OnClickListener castListClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Utils.openImdbForPerson(context, (String) v.getTag());
        }
    };

    castListView.removeAllViews();
    int numColumns = castListView.getColumnCount();

    int layoutMarginPx = 2 * resources.getDimensionPixelSize(R.dimen.remote_content_hmargin);
    int imageMarginPx = 2 * resources.getDimensionPixelSize(R.dimen.image_grid_margin);
    int imageWidth = (displayMetrics.widthPixels - layoutMarginPx - numColumns * imageMarginPx) / numColumns;
    int imageHeight = (int) (imageWidth * 1.2);

    List<VideoType.Cast> noPicturesCastList = new ArrayList<VideoType.Cast>();
    int maxCastPictures = Settings.DEFAULT_MAX_CAST_PICTURES;
    int currentPictureNumber = 0;
    for (int i = 0; i < castList.size(); i++) {
        VideoType.Cast actor = castList.get(i);

        if (((maxCastPictures == -1) || (currentPictureNumber < maxCastPictures))
                && (actor.thumbnail != null)) {
            // Present the picture
            currentPictureNumber++;
            View castView = LayoutInflater.from(context).inflate(R.layout.grid_item_cast, castListView, false);
            ImageView castPicture = (ImageView) castView.findViewById(R.id.picture);
            TextView castName = (TextView) castView.findViewById(R.id.name);
            TextView castRole = (TextView) castView.findViewById(R.id.role);

            castView.getLayoutParams().width = imageWidth;
            castView.getLayoutParams().height = (int) (imageHeight * 1.2);
            castView.setTag(actor.name);
            castView.setOnClickListener(castListClickListener);

            castName.setText(actor.name);
            castRole.setText(actor.role);
            UIUtils.loadImageWithCharacterAvatar(context, hostManager, actor.thumbnail, actor.name, castPicture,
                    imageWidth, imageHeight);
            castListView.addView(castView);
        } else {
            noPicturesCastList.add(actor);
        }
    }

    // Additional cast
    if (noPicturesCastList.size() > 0) {
        additionalCastTitleView.setVisibility(View.VISIBLE);
        additionalCastView.setVisibility(View.VISIBLE);
        StringBuilder castListText = new StringBuilder();
        boolean first = true;
        for (VideoType.Cast cast : noPicturesCastList) {
            if (!first)
                castListText.append("\n");
            first = false;
            if (!TextUtils.isEmpty(cast.role)) {
                castListText.append(
                        String.format(context.getString(R.string.cast_list_text), cast.name, cast.role));
            } else {
                castListText.append(cast.name);
            }
        }
        additionalCastView.setText(castListText);
    } else {
        additionalCastTitleView.setVisibility(View.GONE);
        additionalCastView.setVisibility(View.GONE);
    }
}

From source file:fr.cph.chicago.util.Util.java

public static int[] getScreenSize(@NonNull final Context context) {
    final WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    final Display display = wm.getDefaultDisplay();
    final Point size = new Point();
    display.getSize(size);//w w  w  . j a v  a  2 s  .  co  m
    return new int[] { size.x, size.y };
}

From source file:com.arisprung.tailgate.utilities.FacebookImageLoader.java

public static int calculateDensityDpi(Context context) {
    if (mDensityDpi > 0) {
        // we've already calculated it
        return mDensityDpi;
    }/*  ww w.j  a va  2 s .  c  o  m*/

    if (Integer.parseInt(Build.VERSION.SDK) <= 3) {
        // 1.5 devices are all medium density
        mDensityDpi = DENSITY_MEDIUM;
        return mDensityDpi;
    }

    DisplayMetrics metrics = new DisplayMetrics();
    WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    wm.getDefaultDisplay().getMetrics(metrics);
    int reflectedDensityDpi = DENSITY_MEDIUM;

    // try
    // {
    // reflectedDensityDpi = DisplayMetrics.class.getDeclaredField("mDensityDpi").getInt(metrics);
    // }
    // catch (Exception e)
    // {
    // e.printStackTrace();
    // }

    mDensityDpi = reflectedDensityDpi;
    return mDensityDpi;
}

From source file:com.yamin.kk.vlc.Util.java

public static int convertPxToDp(int px) {
    WindowManager wm = (WindowManager) VLCApplication.getAppContext().getSystemService(Context.WINDOW_SERVICE);
    Display display = wm.getDefaultDisplay();
    DisplayMetrics metrics = new DisplayMetrics();
    display.getMetrics(metrics);//  w ww . j  a v  a2  s . c om
    float logicalDensity = metrics.density;
    int dp = Math.round(px / logicalDensity);
    return dp;
}