dimensions.js 1009 B

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