溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Android如何實(shí)現(xiàn)圖片顯示與屏幕適配

發(fā)布時(shí)間:2021-07-13 10:45:14 來源:億速云 閱讀:136 作者:小新 欄目:移動(dòng)開發(fā)

這篇文章主要介紹Android如何實(shí)現(xiàn)圖片顯示與屏幕適配,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!

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如何實(shí)現(xiàn)圖片顯示與屏幕適配”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對大家有幫助,更多相關(guān)知識,歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI