zoukankan      html  css  js  c++  java
  • Android万能分辨率适应法

    (1)获取屏幕的尺寸

    WindowManager windowManager = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
    Display d = windowManager.getDefaultDisplay();
    mWidth = d.getWidth();
    mHeight = d.getHeight();
    DisplayMetrics dm = getResources().getDisplayMetrics();
    mScreenDensity = dm.density;

    (2)美工设计图的尺寸

    uiWidth,uiHeight

    (3)获取缩放比例

    float scaleWidth = mWidth / uiWidth;
    float scaleHeight = mHeight/ uiHeight; 

    (4)所有布局的尺寸用代码实现:

    public static int getWidthSize(int size){
        return (int) (size * scaleWidth);
    }
    
    public static int getHightSize(int size){
        return (int) (size * scaleHeight);
    }
    
    public static float getTextSize(int pxSize){
        return (pxSize*scaleHeight) / mScreenDensity;
    }
    
    public static void setViewSize(int width, int height, View v){
        int paramWidth = getWidthSize(width);
        int paramHeight = getHightSize(height);
        ViewGroup.MarginLayoutParams params 
               = (ViewGroup.MarginLayoutParams) v.getLayoutParams();
        if (width != INVALID){
            params.width = paramWidth;
        }
        if (height != INVALID){
            params.height = paramHeight;
        }
        v.setLayoutParams(params);
    }
    
    public static void setViewPadding(int left, int top, int right, int bottom,
            View v){
        left = getWidthSize(left);
        top = getHightSize(top);
        right = getWidthSize(right);
        bottom = getWidthSize(bottom);
        v.setPadding(left, top, right, bottom);
    }
    
    
    public static void setViewMargin(int left, int top, int right, int bottom,
            View v){
        int paramLeft = getWidthSize(left);
        int paramTop =  getHightSize(top);
        int paramRight = getWidthSize(right);
        int paramBottom = getHightSize(bottom);
        ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams)
                     v.getLayoutParams();
        if (left != INVALID){
            params.leftMargin = paramLeft;
        }
        if (right != INVALID){
            params.rightMargin = paramRight;
        }
        if (top != INVALID){
            params.topMargin = paramTop;
        }
        if (bottom != INVALID){
            params.bottomMargin = paramBottom;
        }
        v.setLayoutParams(params);
    }
  • 相关阅读:
    nullnull聚类小文(二)
    classjavaCore Java Question List #6
    设置仿真器HJTAG ARM仿真器和MDK 联调设置
    二分图判断hdu 1829 A Bug's Life
    信息掩码游戏地图掩码相关(msk)
    导航accessibilityAndroid 抽屉导航
    算法nodehdu 2112 hdu today
    链接函数hdu 1234开门人和关门人
    分配器内存一步一步写STL:空间配置器(1)
    程序连接如何使用JLink V8 烧写程序到NOR Flash
  • 原文地址:https://www.cnblogs.com/vijay/p/3740792.html
Copyright © 2011-2022 走看看