dimensions.js 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. // let console = require('console');
  2. let {Dimensions,StatusBar,Platform,PixelRatio} = require('react-native');
  3. let {width,scale,height,fontScale} = Dimensions.get("window");
  4. let isIOS = Platform.OS == "ios";
  5. let statusBarHeight = isIOS? 20: StatusBar.currentHeight;
  6. let pixelRatio = PixelRatio;
  7. fontScale = scale;
  8. const widthScale = width / 375;
  9. const heightScale = height / 667;
  10. /*
  11. * 设计稿元素宽度(px) / 设计稿总宽度(px) = 元素的宽度(dp) / 屏幕的总宽度(dp)
  12. * 358 / 375 = x / width
  13. */
  14. // console.log('widthScale',widthScale)
  15. export default Dimensions = {
  16. get:Dimensions.get,
  17. screenWidth:width,
  18. screenHeight:height,
  19. screenScale:scale,
  20. width:width,
  21. height:height,
  22. scale:scale,
  23. fontScale:fontScale,
  24. statusBarHeight:statusBarHeight,
  25. contentHeight:height-statusBarHeight,
  26. pixelRatio: pixelRatio,
  27. getFontSize:function(size){
  28. return size*fontScale;
  29. },
  30. getWidth:function(width){
  31. return width*widthScale;
  32. },
  33. getHeight:function(height){
  34. return height*heightScale;
  35. },
  36. /* px转dp */
  37. getDp: px => PixelRatio.roundToNearestPixel(px),
  38. size:{
  39. "1":1*fontScale
  40. }
  41. }