這篇文章主要介紹Android如何實(shí)現(xiàn)圖片顯示與屏幕適配,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!
站在用戶的角度思考問題,與客戶深入溝通,找到易門網(wǎng)站設(shè)計(jì)與易門網(wǎng)站推廣的解決方案,憑借多年的經(jīng)驗(yàn),讓設(shè)計(jì)與互聯(lián)網(wǎng)技術(shù)結(jié)合,創(chuàng)造個性化、用戶體驗(yàn)好的作品,建站類型包括:成都做網(wǎng)站、網(wǎng)站設(shè)計(jì)、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣、主機(jī)域名、網(wǎng)頁空間、企業(yè)郵箱。業(yè)務(wù)覆蓋易門地區(qū)。
Android 圖片顯示與屏幕適配的問題
在Android開發(fā)中比較頭疼的是Android的分辨率問題,那么這里給大家介紹個萬能辦法,這個辦法的優(yōu)點(diǎn)是可以實(shí)現(xiàn)萬能適應(yīng),給開發(fā)和美工設(shè)計(jì)提供了依據(jù),但是對開發(fā)來說代碼量也不少,具體辦法:
(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)美工設(shè)計(jì)圖的尺寸
uiWidth,uiHeight
(3)獲取縮放比例
float scaleWidth = mWidth / uiWidth; float scaleHeight = mHeight/ uiHeight;
(4)所有布局的尺寸用代碼實(shí)現(xiàn):
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)這里是設(shè)置尺寸的代碼:
setViewSize(100, 100, mView); setViewMargin(20, 0, 0, 20, mView); setViewPadding(10, 10, 10, 10, mView); mTextView.setTextSize(getTextSize(30));
由上在設(shè)計(jì)效果圖時,可對圖內(nèi)元素進(jìn)行尺寸標(biāo)注,程序即可實(shí)現(xiàn)按比例縮放。
以上是“Android如何實(shí)現(xiàn)圖片顯示與屏幕適配”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對大家有幫助,更多相關(guān)知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!
本文標(biāo)題:Android如何實(shí)現(xiàn)圖片顯示與屏幕適配
分享路徑:http://muchs.cn/article44/gecdee.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供建站公司、網(wǎng)站設(shè)計(jì)、面包屑導(dǎo)航、外貿(mào)網(wǎng)站建設(shè)、商城網(wǎng)站、ChatGPT
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)