React Native comes with a PixelRatio module that has a method called get
. You must manually divide all of your measurements by the pixel ratio.
- Start with the number of physical pixels.
- Divide it by the pixel ratio (
PixelRatio.get()
) to get the corresponding number of logical pixels.
- Use the number of logical pixels in
StyleSheet.create
and other layout-related methods.
- React Native and the underlying native UI frameworks will convert the logical pixels back to physical pixels.
You could write a helper method for this if it were to get tedious. This is an idea for an API:
StyleSheet.create({
box: {
width: physical(100),
height: physical(75),
},
});
Most of the time though you should work with logical pixels. It's only when you want to precisely control the hardware screen or are interacting with a system without a notion of screen scale (e.g. fetching a JPEG image) that you'd want to work with physical pixels.