Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android 獲取屏幕信息

Android 獲取屏幕信息

編輯:關於Android編程

 

獲取屏幕信息:狀態欄高度、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;
    }


 

 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved