Example usage for android.view Display getMetrics

List of usage examples for android.view Display getMetrics

Introduction

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

Prototype

public void getMetrics(DisplayMetrics outMetrics) 

Source Link

Document

Gets display metrics that describe the size and density of this display.

Usage

From source file:com.appassit.common.Utils.java

public static float sp2px(Context context, float sp) {
    WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    Display display = wm.getDefaultDisplay();
    DisplayMetrics displaymetrics = new DisplayMetrics();
    display.getMetrics(displaymetrics);

    final float scale = displaymetrics.scaledDensity;
    return sp * scale;
}

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

/**
 * ??//ww  w . j  a v  a  2  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.example.textviewsample.MainActivityFragment.java

@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
@Override/*from  www.  ja v  a2 s  .c  om*/
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    sourceCodeText = (TextView) view.findViewById(R.id.textView7);
    DisplayMetrics metrics = new DisplayMetrics();
    WindowManager wm = (WindowManager) getActivity().getSystemService(Context.WINDOW_SERVICE);
    Display display = wm.getDefaultDisplay();
    display.getMetrics(metrics);

    int widthInPixels = metrics.widthPixels;
    int heightInPixels = metrics.heightPixels;

    float scaleFactor = metrics.density;

    float widthDp = widthInPixels / scaleFactor;
    float heightDp = heightInPixels / scaleFactor;

    TextView tv1 = (TextView) view.findViewById(R.id.textView1);
    tv1.setText(String.format(Locale.getDefault(),
            "Density: %s, sw%s, w%s\nDevice year: " + "%d\nCPU " + "Cores: %d, " + "MaxFreq: %d",
            getString(R.string.density), DisplayUtils.getSmallestWidth(getActivity()), widthDp,
            YearClass.get(getActivity()), DeviceInfo.getNumberOfCPUCores(), DeviceInfo.getCPUMaxFreqKHz()));

    ExpandableTextView etv = (ExpandableTextView) view.findViewById(R.id.textView6);
    etv.setContracted(true);

    new Shimmer().setDuration(1500).setStartDelay(4000).setRepeatDelay(5000)
            .start((ShimmerTextView) view.findViewById(R.id.textView12));
}

From source file:com.devbrackets.android.exomedia.ui.animation.BottomViewHideShowAnimation.java

private int getHideShowDelta(View view) {
    DisplayMetrics displayMetrics = new DisplayMetrics();
    Display display = ((WindowManager) view.getContext().getSystemService(Context.WINDOW_SERVICE))
            .getDefaultDisplay();/*from   ww w. ja v  a 2s.  c om*/
    display.getMetrics(displayMetrics);

    int screenHeight = displayMetrics.heightPixels;
    return screenHeight - view.getTop();
}

From source file:org.chromium.ChromeSystemDisplay.java

private float getDpiX(final Display display) {
    final DisplayMetrics displayMetrics = new DisplayMetrics();
    display.getMetrics(displayMetrics);

    // displayMetrics.xdpi is not reliable.
    return displayMetrics.densityDpi;
}

From source file:org.chromium.ChromeSystemDisplay.java

private float getDpiY(final Display display) {
    final DisplayMetrics displayMetrics = new DisplayMetrics();
    display.getMetrics(displayMetrics);

    // displayMetrics.ydpi is not reliable.
    return displayMetrics.densityDpi;
}

From source file:com.fallahpoor.infocenter.fragments.ScreenFragment.java

private ArrayList<String> getSubItemsArrayList() {

    ArrayList<String> subItems = new ArrayList<>();
    Point displaySize = new Point();
    DisplayMetrics displayMetrics = new DisplayMetrics();
    Display display = getActivity().getWindowManager().getDefaultDisplay();

    display.getSize(displaySize);/*www .ja  va2s  .c o m*/
    display.getMetrics(displayMetrics);

    subItems.add(getDisplaySize(displaySize));
    subItems.add(getOrientation(displaySize));
    subItems.add(getRefreshRate(display));
    subItems.add(getDpi(displayMetrics));
    subItems.add(getHorizontalDpi(displayMetrics));
    subItems.add(getVerticalDpi(displayMetrics));

    return subItems;

}

From source file:org.acra.collector.DisplayManagerCollector.java

@NonNull
private Object collectDisplayData(@NonNull Display display) {
    final DisplayMetrics metrics = new DisplayMetrics();
    display.getMetrics(metrics);

    //noinspection deprecation
    return collectCurrentSizeRange(display) + collectFlags(display) + display.getDisplayId() + ".height="
            + display.getHeight() + '\n' + collectMetrics(display) + collectName(display)
            + display.getDisplayId() + ".orientation=" + display.getRotation() + '\n' + display.getDisplayId()
            + ".pixelFormat=" + display.getPixelFormat() + '\n' + collectRealMetrics(display)
            + collectRealSize(display) + collectRectSize(display) + display.getDisplayId() + ".refreshRate="
            + display.getRefreshRate() + '\n' + collectRotation(display) + collectSize(display)
            + display.getDisplayId() + ".width=" + display.getWidth() + '\n' + collectIsValid(display);
}

From source file:com.mruddy.devdataviewer.DevDataListFragment.java

/**
 * @return//from  w  ww .jav a 2 s.  co  m
 */
@SuppressLint("NewApi")
private List<String> getDevDataList() {
    final List<String> result = new LinkedList<String>();
    final Display display = this.getActivity().getWindowManager().getDefaultDisplay();
    final DisplayMetrics appAreaDisplayMetrics = new DisplayMetrics();
    display.getMetrics(appAreaDisplayMetrics);
    result.add("API Level: " + Build.VERSION.SDK_INT);
    result.add("Android Release: " + Build.VERSION.RELEASE);
    if (Build.VERSION.SDK_INT >= 17) {
        final DisplayMetrics fullDisplayMetrics = new DisplayMetrics();
        display.getRealMetrics(fullDisplayMetrics);
        result.add("Full Width (px): " + fullDisplayMetrics.widthPixels);
        result.add("Full Height (px): " + fullDisplayMetrics.heightPixels);
        result.add("Full Width (dp): " + ((int) (fullDisplayMetrics.widthPixels / fullDisplayMetrics.density)));
        result.add(
                "Full Height (dp): " + ((int) (fullDisplayMetrics.heightPixels / fullDisplayMetrics.density)));
    }
    result.add("App Area Width (px): " + appAreaDisplayMetrics.widthPixels);
    result.add("App Area Height (px): " + appAreaDisplayMetrics.heightPixels);
    result.add("App Area Width (dp): "
            + ((int) (appAreaDisplayMetrics.widthPixels / appAreaDisplayMetrics.density)));
    result.add("App Area Height (dp): "
            + ((int) (appAreaDisplayMetrics.heightPixels / appAreaDisplayMetrics.density)));
    result.add("dp Scaling Factor: " + appAreaDisplayMetrics.density);
    final String densityBucket = DevDataListFragment.DENSITY_BUCKETS.get(appAreaDisplayMetrics.densityDpi);
    result.add("Density Bucket: " + (null != densityBucket ? densityBucket : "other") + " ("
            + appAreaDisplayMetrics.densityDpi + ")");
    result.add("Actual xdpi: " + appAreaDisplayMetrics.xdpi);
    result.add("Actual ydpi: " + appAreaDisplayMetrics.ydpi);
    if (Build.VERSION.SDK_INT >= 8) {
        final String rotation = DevDataListFragment.ROTATION.get(display.getRotation());
        result.add("Rotation: " + (null != rotation ? rotation : "other (" + display.getRotation() + ")"));
    }
    final String orientation = DevDataListFragment.ORIENTATION
            .get(this.getResources().getConfiguration().orientation);
    result.add("Orientation: " + (null != orientation ? orientation
            : "other (" + this.getResources().getConfiguration().orientation + ")"));
    final int screenSizeKey = this.getResources().getConfiguration().screenLayout
            & Configuration.SCREENLAYOUT_SIZE_MASK;
    final String screenSizeBucket = DevDataListFragment.SCREEN_SIZE_BUCKETS.get(screenSizeKey);
    result.add(
            "Size Bucket: " + (null != screenSizeBucket ? screenSizeBucket : "other (" + screenSizeKey + ")"));
    return result;
}

From source file:org.chromium.ChromeSystemDisplay.java

private JSONObject getWorkArea(final Display display) throws JSONException {
    JSONObject ret = new JSONObject();
    final DisplayMetrics displayMetrics = new DisplayMetrics();
    display.getMetrics(displayMetrics);

    ret.put("left", 0);
    ret.put("top", 0);
    ret.put("width", displayMetrics.widthPixels);
    ret.put("height", displayMetrics.heightPixels);

    return ret;/*from  w  w  w. jav a 2s.c o  m*/
}