Android universal resolution adaptation method, android adaptation

Source: Internet
Author: User

Android universal resolution adaptation method, android adaptation
(1) obtain the screen size

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) The size of the artist's Design Drawing 
uiWidth,uiHeight
(3) Get the zoom Ratio
float scaleWidth = mWidth / uiWidth;float scaleHeight = mHeight/ uiHeight; 

(4) code implementation for all layout sizes
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);}






In android, how does one adapt your program to various resolutions?

Drawable-ldpi
Drawable-mdpi
Drawable-hdpi
Drawable-xhdpi
These enable you to put images of different resolutions based on the resolution.
Values-ldpi
Values-mdpi
...
These values are set based on different resolutions.
Layout-ldpi
Layout-mdpi
....
These are different resolutions and adopt different la S.
It is troublesome to adapt to all resolutions.

Android app resolution Adaptive Guidance ideas!

RelativeLayout
Then the pixel is dip
Also, layout_weight sets the ratio of each layout.
Hope to help you

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.