• Android万能分辨率适应法


    在Android开发中比较头疼的是Android的分辨率问题,那么这里给大家介绍个万能办法,这个办法的优点是可以实现万能适应,给开发和美工设计提供了依据,但是对开发来说代码量也不少,具体办法你听我来说:

    (1)获取屏幕的尺寸

    1WindowManager windowManager = (WindowManager)
    2         getSystemService(Context.WINDOW_SERVICE);
    3 Display d = windowManager.getDefaultDisplay();
    4 mWidth = d.getWidth();
    5 mHeight = d.getHeight();
    6 DisplayMetrics dm = getResources().getDisplayMetrics();
    7 mScreenDensity = dm.density;

    (2)美工设计图的尺寸

      uiWidth,uiHeight

    (3)获取缩放比例

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

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

    01public static int getWidthSize(int size){
    02    return (int) (size * scaleWidth);
    03}
    04 
    05public static int getHightSize(int size){
    06    return (int) (size * scaleHeight);
    07}
    08 
    09public static float getTextSize(int pxSize){
    10    return (pxSize*scaleHeight) / mScreenDensity;
    11}
    12 
    13public static void setViewSize(int width, int height, View v){
    14    int paramWidth = getWidthSize(width);
    15    int paramHeight = getHightSize(height);
    16    ViewGroup.MarginLayoutParams params
    17           = (ViewGroup.MarginLayoutParams) v.getLayoutParams();
    18    if (width != INVALID){
    19        params.width = paramWidth;
    20    }
    21    if (height != INVALID){
    22        params.height = paramHeight;
    23    }
    24    v.setLayoutParams(params);
    25}
    26 
    27public static void setViewPadding(int left, int top, int right, int bottom,
    28        View v){
    29    left = getWidthSize(left);
    30    top = getHightSize(top);
    31    right = getWidthSize(right);
    32    bottom = getWidthSize(bottom);
    33    v.setPadding(left, top, right, bottom);
    34}
    35 
    36 
    37public static void setViewMargin(int left, int top, int right, int bottom,
    38        View v){
    39    int paramLeft = getWidthSize(left);
    40    int paramTop =  getHightSize(top);
    41    int paramRight = getWidthSize(right);
    42    int paramBottom = getHightSize(bottom);
    43    ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams)
    44                 v.getLayoutParams();
    45    if (left != INVALID){
    46        params.leftMargin = paramLeft;
    47    }
    48    if (right != INVALID){
    49        params.rightMargin = paramRight;
    50    }
    51    if (top != INVALID){
    52        params.topMargin = paramTop;
    53    }
    54    if (bottom != INVALID){
    55        params.bottomMargin = paramBottom;
    56    }
    57    v.setLayoutParams(params);
    58}

        (5)这里是设置尺寸的代码:

    1setViewSize(100100, mView);
    2setViewMargin(200020, mView);
    3setViewPadding(10101010, mView);
    4mTextView.setTextSize(getTextSize(30));

        由上在设计效果图时,可对图内元素进行尺寸标注,程序即可实现按比例缩放。

  • 相关阅读:
    第七讲 宋词:婉约之曲与豪放之声
    P2024 食物链
    可以吹一年的事
    信息传递
    11.11模拟赛总结(又名斗地主战记)
    11.9模拟赛总结
    扩展欧几里得(exgcd模板)
    发糖果(拓扑排序模板)
    高斯消元
    关于我
  • 原文地址:https://www.cnblogs.com/wsfjlagr/p/3702558.html
Copyright © 2020-2023  润新知