Няма описание

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. import {
  2. Dimensions,
  3. PixelRatio,
  4. } from 'react-native';
  5. export const deviceWidth = Dimensions.get('window').width; //设备的宽度
  6. export const deviceHeight = Dimensions.get('window').height; //设备的高度
  7. let fontScale = PixelRatio.getFontScale(); //返回字体大小缩放比例
  8. let pixelRatio = PixelRatio.get(); //当前设备的像素密度
  9. const defaultPixel = 2; //iphone6的像素密度
  10. //px转换成dp
  11. const w2 = 750 / defaultPixel;
  12. const h2 = 1334 / defaultPixel;
  13. const scale = Math.min(deviceHeight / h2, deviceWidth / w2); //获取缩放比例
  14. /**
  15. * 设置text为sp
  16. * @param size sp
  17. * return number dp
  18. */
  19. export function setSpText(size: number) {
  20. size = Math.round((size * scale ) * pixelRatio / fontScale);
  21. return size / defaultPixel;
  22. }
  23. export function scaleSize(size: number) {
  24. size = Math.round(size * scale );
  25. return size / defaultPixel;
  26. }
  27. export function widthRatio(ratio){
  28. if(ratio>1)return ''
  29. return Math.round(ratio*deviceWidth)
  30. }
  31. export function heightRatio(ratio){
  32. if(ratio>1)return ''
  33. return Math.round(ratio*deviceHeight)
  34. }
  35. export function widthPxToDp(value){
  36. const deviceWidthDp = Dimensions.get('window').width;
  37. const uiWidthPx = 750;//ps图比例
  38. return value * deviceWidthDp / uiWidthPx;
  39. }
  40. export function heightPxToDp(value){
  41. const den = PixelRatio.get();
  42. const deviceHeightDp = Dimensions.get('window').height;
  43. const uiHeightPx = 1344;//ps图比例
  44. // console.log('height:',deviceHeightDp,'translate:',value * deviceHeightDp / uiHeightPx)
  45. return value * deviceHeightDp / uiHeightPx;
  46. }