• 获取Android设备屏幕分辨率


    1.Android 4.3引入的wm工具:

    a.获取Android设备屏幕分辨率: adb shell wm size

    b.获取android设备屏幕密度: adb shell wm density

    Wm.java

    public class Wm extends BaseCommand {
    ...
    public void onShowUsage(PrintStream out) {
            out.println(
                    "usage: wm [subcommand] [options]
    " +
                    "       wm size [reset|WxH]
    " +
                    "       wm density [reset|DENSITY]
    " +
                    "       wm overscan [reset|LEFT,TOP,RIGHT,BOTTOM]
    " +
                    "
    " +
                    "wm size: return or override display size.
    " +
                    "
    " +
                    "wm density: override display density.
    " +
                    "
    " +
                    "wm overscan: set overscan area for display.
    "
                    );
        }
    
        public void onRun() throws Exception {
            mWm = IWindowManager.Stub.asInterface(ServiceManager.checkService(
                            Context.WINDOW_SERVICE));
            if (mWm == null) {
                System.err.println(NO_SYSTEM_ERROR_CODE);
                throw new AndroidException("Can't connect to window manager; is the system running?");
            }
    
            String op = nextArgRequired();
    
            if (op.equals("size")) {
                runDisplaySize();
            } else if (op.equals("density")) {
                runDisplayDensity();
            } else if (op.equals("overscan")) {
                runDisplayOverscan();
            } else {
                showError("Error: unknown command '" + op + "'");
                return;
            }
        }
    
        private void runDisplaySize() throws Exception {
            String size = nextArg();
            int w, h;
            if (size == null) {
                Point initialSize = new Point();
                Point baseSize = new Point();
                try {
                    mWm.getInitialDisplaySize(Display.DEFAULT_DISPLAY, initialSize);
                    mWm.getBaseDisplaySize(Display.DEFAULT_DISPLAY, baseSize);
                    System.out.println("Physical size: " + initialSize.x + "x" + initialSize.y);
                    if (!initialSize.equals(baseSize)) {
                        System.out.println("Override size: " + baseSize.x + "x" + baseSize.y);
                    }
                } catch (RemoteException e) {
                }
                return;
            } else if ("reset".equals(size)) {
                w = h = -1;
            } else {
                int div = size.indexOf('x');
                if (div <= 0 || div >= (size.length()-1)) {
                    System.err.println("Error: bad size " + size);
                    return;
                }
                String wstr = size.substring(0, div);
                String hstr = size.substring(div+1);
                try {
                    w = Integer.parseInt(wstr);
                    h = Integer.parseInt(hstr);
                } catch (NumberFormatException e) {
                    System.err.println("Error: bad number " + e);
                    return;
                }
            }
    
            try {
                if (w >= 0 && h >= 0) {
                    // TODO(multidisplay): For now Configuration only applies to main screen.
                    mWm.setForcedDisplaySize(Display.DEFAULT_DISPLAY, w, h);
                } else {
                    mWm.clearForcedDisplaySize(Display.DEFAULT_DISPLAY);
                }
            } catch (RemoteException e) {
            }
        }
    
        private void runDisplayDensity() throws Exception {
            String densityStr = nextArg();
            int density;
            if (densityStr == null) {
                try {
                    int initialDensity = mWm.getInitialDisplayDensity(Display.DEFAULT_DISPLAY);
                    int baseDensity = mWm.getBaseDisplayDensity(Display.DEFAULT_DISPLAY);
                    System.out.println("Physical density: " + initialDensity);
                    if (initialDensity != baseDensity) {
                        System.out.println("Override density: " + baseDensity);
                    }
                } catch (RemoteException e) {
                }
                return;
            } else if ("reset".equals(densityStr)) {
                density = -1;
            } else {
                try {
                    density = Integer.parseInt(densityStr);
                } catch (NumberFormatException e) {
                    System.err.println("Error: bad number " + e);
                    return;
                }
                if (density < 72) {
                    System.err.println("Error: density must be >= 72");
                    return;
                }
            }
    
            try {
                if (density > 0) {
                    // TODO(multidisplay): For now Configuration only applies to main screen.
                    mWm.setForcedDisplayDensity(Display.DEFAULT_DISPLAY, density);
                } else {
                    mWm.clearForcedDisplayDensity(Display.DEFAULT_DISPLAY);
                }
            } catch (RemoteException e) {
            }
        }
    ...
    View Code

    WindowManagerService.java

    View Code

    DisplayContent.java

    DisplayManager.java

    View Code

    2.显示区域分为应用显示区域和真实显示区域。

    a.应用显示区域不包括系统点缀,它可能比真实显示区域小因为系统减掉了点缀元素的空间如导航栏(隐藏/显示导航栏得到的高度不同)

    View Code

    Display.java

    View Code

    DisplayInfo.java

    View Code

    b.真实显示区域包括系统点缀,但它有可能比物理显示小当窗口管理器模拟更小显示(如使用adb shell am display-size)时

    View Code
  • 相关阅读:
    关于Web 打印的三种方法
    禁止另存为 禁止复制 禁止右键 JS
    无限数据递归
    VS2003+Ajax Div文本框输入提示
    C#关于日期月天数和一年有多少周及某年某周时间段的计算
    git代码冲突
    RabbitMQ死信队列
    Elementui:ElContainer布局
    mxml调用as
    用自己的MapServer,解决沙箱冲突
  • 原文地址:https://www.cnblogs.com/fanfeng/p/3263853.html
Copyright © 2020-2023  润新知