
這篇文章主要介紹Android如何實現圖片顯示與屏幕適配,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!
Android 圖片顯示與屏幕適配的問題
在Android開發中比較頭疼的是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);}(5)這里是設置尺寸的代碼:
setViewSize(100, 100, mView); setViewMargin(20, 0, 0, 20, mView); setViewPadding(10, 10, 10, 10, mView); mTextView.setTextSize(getTextSize(30));
由上在設計效果圖時,可對圖內元素進行尺寸標注,程序即可實現按比例縮放。
以上是“Android如何實現圖片顯示與屏幕適配”這篇文章的所有內容,感謝各位的閱讀!希望分享的內容對大家有幫助,更多相關知識,歡迎關注創新互聯行業資訊頻道!
新聞名稱:Android如何實現圖片顯示與屏幕適配-創新互聯
本文路徑:http://www.yijiale78.com/article30/phipo.html
成都網站建設公司_創新互聯,為您提供網站改版、服務器托管、ChatGPT、網站收錄、虛擬主機、云服務器
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯