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