Android 擷取螢幕資訊

來源:互聯網
上載者:User

標籤:screen   oncreate   title   content   height   


擷取螢幕資訊:狀態列高度、content大小


public void getScreenInfo(final Activity activity){        final View v = activity.getWindow().findViewById(Window.ID_ANDROID_CONTENT);        v.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {            @Override            public boolean onPreDraw() {                Rect frame = new Rect();                activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);                int statusBarHeight = frame.top;                v.getViewTreeObserver().removeOnPreDrawListener(this);                Log.e("", "ScreenInfo===ContentVIew===Height:" + v.getHeight() + "  Width:" + v.getWidth() + " Top:" + v.getTop() + "  Left:" + v.getLeft()+"   Bottom:"+v.getBottom()+"   狀態列高度:"+statusBarHeight);                return true;            }        });    }

其他的一些工具類:擷取螢幕大小、密度、橫屏豎屏


private static int widthPixels = 0;    private static int heightPixels = 0;    private static float density = 0;    public static DisplayMetrics getMetrics(Context context) {        DisplayMetrics metrics = new DisplayMetrics();        getWindowManager(context).getDefaultDisplay().getMetrics(metrics);        return metrics;    }    public static String getOrientation(Context context) {        switch (getWindowManager(context).getDefaultDisplay().getRotation()) {            case Surface.ROTATION_0:                return "portrait";            case Surface.ROTATION_90:                return "landscape";            case Surface.ROTATION_180:                return "reverse portrait";            default:                return "reverse landscape";        }    }    public static WindowManager getWindowManager(Context context) {        return (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);    }    public static int getWidth(Context context) {        if (widthPixels == 0) {            widthPixels = getMetrics(context).widthPixels;        }        return widthPixels;    }    public static int getHeight(Context context) {        if (heightPixels == 0) {            ViewConfiguration.get(context).hasPermanentMenuKey();            heightPixels = getMetrics(context).heightPixels;        }        return heightPixels;    }    public static float getDensity(Context context) {        if (density == 0) {            density = getMetrics(context).density;        }        return density;    }



Android 擷取螢幕資訊

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.