react-native 適配不同手機(jī)屏幕

前提:設(shè)計(jì)圖為iphone6尺寸(750*1334),像素密度默認(rèn)為2

相關(guān)代碼:

import {

? ? ?Dimensions,

? ? ?PixelRatio,

} from 'react-native';

export const deviceWidth = Dimensions.get('window').width;? ? ? //設(shè)備的寬度

export const deviceHeight = Dimensions.get('window').height;? ? //設(shè)備的高度


let pixelRatio = PixelRatio.get();? ? ? //當(dāng)前設(shè)備的像素密度

const defaultPixel = 2;? ? ? ? ? ? ? ? ? ? ? ? ? //iphone6的像素密度

//px轉(zhuǎn)換成dp

const w2 = 750 / defaultPixel;

const h2 = 1334 / defaultPixel;

const scale = Math.min(deviceHeight / h2, deviceWidth / w2);? //獲取縮放比例


export function scaleSize(size: number) {

? ? ? ? ?size = Math.round(size * scale + 0.5);

? ? ? ? ?return size;

}


使用時(shí):

fontSize:scaleSize(14)

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容