1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253 |
- import {
- Dimensions,
- PixelRatio,
- } from 'react-native';
-
-
- export const deviceWidth = Dimensions.get('window').width; //设备的宽度
- export const deviceHeight = Dimensions.get('window').height; //设备的高度
- let fontScale = PixelRatio.getFontScale(); //返回字体大小缩放比例
-
- let pixelRatio = PixelRatio.get(); //当前设备的像素密度
- const defaultPixel = 2; //iphone6的像素密度
- //px转换成dp
- const w2 = 750 / defaultPixel;
- const h2 = 1334 / defaultPixel;
- const scale = Math.min(deviceHeight / h2, deviceWidth / w2); //获取缩放比例
- /**
- * 设置text为sp
- * @param size sp
- * return number dp
- */
- export function setSpText(size: number) {
- size = Math.round((size * scale ) * pixelRatio / fontScale);
- return size / defaultPixel;
- }
-
- export function scaleSize(size: number) {
-
- size = Math.round(size * scale );
- return size / defaultPixel;
- }
-
- export function widthRatio(ratio){
- if(ratio>1)return ''
- return Math.round(ratio*deviceWidth)
- }
- export function heightRatio(ratio){
- if(ratio>1)return ''
- return Math.round(ratio*deviceHeight)
- }
- export function widthPxToDp(value){
- const deviceWidthDp = Dimensions.get('window').width;
- const uiWidthPx = 750;//ps图比例
- return value * deviceWidthDp / uiWidthPx;
- }
- export function heightPxToDp(value){
- const den = PixelRatio.get();
- const deviceHeightDp = Dimensions.get('window').height;
- const uiHeightPx = 1344;//ps图比例
- // console.log('height:',deviceHeightDp,'translate:',value * deviceHeightDp / uiHeightPx)
- return value * deviceHeightDp / uiHeightPx;
- }
|