Android 圖片顯示與屏幕適配的問題
Android 圖片顯示與屏幕適配的問題
在Android開發(fā)中比較頭疼的是Android的分辨率問題,那么這里給大家介紹個(gè)萬能辦法,這個(gè)辦法的優(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ì)效果圖時(shí),可對圖內(nèi)元素進(jìn)行尺寸標(biāo)注,程序即可實(shí)現(xiàn)按比例縮放。
以上就是Android 圖片顯示與屏幕適配的問題的萬能解決辦法,如有疑問請留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
相關(guān)文章
Android開發(fā)人臉識別統(tǒng)計(jì)人臉數(shù)
這篇文章主要介紹了Android開發(fā)人臉識別統(tǒng)計(jì)人臉數(shù),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-10-10Kotlin Extension Function擴(kuò)展函數(shù)詳細(xì)介紹
Kotlin支持使用新功能擴(kuò)展類的能力,而無需通過類實(shí)現(xiàn)繼承概念或使用設(shè)計(jì)模式,如裝飾器(Decorator)。這是通過稱為擴(kuò)展功能(Extension Function)的特殊方式來完成的。因此,此功能可以有效地使代碼變得更清晰和易于閱讀,并且還可以減少代碼2023-02-02Android 通過TCP協(xié)議上傳指定目錄文件的方法
這篇文章主要介紹了Android 通過TCP協(xié)議上傳指定目錄文件的方法,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-03-03Android Activity中使用Intent實(shí)現(xiàn)頁面跳轉(zhuǎn)與參數(shù)傳遞的方法
這篇文章主要介紹了Android Activity中使用Intent實(shí)現(xiàn)頁面跳轉(zhuǎn)與參數(shù)傳遞的方法,結(jié)合實(shí)例形式簡單分析了Android中的Activity交互操作相關(guān)技巧,需要的朋友可以參考下2016-07-07Android程序開發(fā)之使用Design包實(shí)現(xiàn)QQ動(dòng)畫側(cè)滑效果和滑動(dòng)菜單導(dǎo)航
這篇文章主要介紹了Android程序開發(fā)之使用Design包實(shí)現(xiàn)QQ動(dòng)畫側(cè)滑效果和滑動(dòng)菜單導(dǎo)航的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-07-07Android實(shí)現(xiàn)通過手勢控制圖片大小縮放的方法
這篇文章主要介紹了Android實(shí)現(xiàn)通過手勢控制圖片大小縮放的方法,結(jié)合實(shí)例形式分析了Android控制圖片縮放的原理、實(shí)現(xiàn)步驟與相關(guān)操作技巧,需要的朋友可以參考下2016-10-10Android用PopupWindow實(shí)現(xiàn)自定義overflow
這篇文章主要介紹了Android用PopupWindow實(shí)現(xiàn)自定義overflow的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-11-11AndroidStudio kotlin配置詳細(xì)介紹
這篇文章主要介紹了AndroidStudio kotlin配置詳細(xì)介紹的相關(guān)資料,需要的朋友可以參考下2017-05-05