2

Based on the React Native documentation, PixelRatio.get() should return one of the following values for the device pixel density:

- PixelRatio.get() === 1
  mdpi Android devices (160 dpi)
- PixelRatio.get() === 1.5
  hdpi Android devices (240 dpi)
- PixelRatio.get() === 2
  iPhone 4, 4S
  iPhone 5, 5c, 5s
  iPhone 6
  xhdpi Android devices (320 dpi)
- PixelRatio.get() === 3
  iPhone 6 plus
  xxhdpi Android devices (480 dpi)
- PixelRatio.get() === 3.5
  Nexus 6

When calling PixelRatio.get() on a Nexus 5x I get the density value 2.625. Is the expected? Are we supposed to manually manage the values that are between the basic ones or is this a bug inside React Native?

FYI, I'm using RN v0.24

2 Answers 2

1

React Native is likely just deferring to the Android density. The Android documentation explains this well:

Each generalized size and density spans a range of actual screen sizes and densities. For example, two devices that both report a screen size of normal might have actual screen sizes and aspect ratios that are slightly different when measured by hand. Similarly, two devices that report a screen density of hdpi might have real pixel densities that are slightly different.

Android will return the actual density of the device. There is room for manufacturers to tweak this, so it's not always the exact pixel density.

0

Here's is how React Native calculates the correct scale:

https://github.com/facebook/react-native/blob/1f8d1002ef991b66f4cac8d8b8689f27a244ac4d/Libraries/Image/AssetSourceResolver.js#L148

You'd use it like this:

AssetSourceResolver.pickScale([1, 1.5, 2, 3], PixelRatio.getScale())

Probably it's better to copy the code out though, because it's not public API.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.