PixelRatio gives access to the device’s pixel density, allowing you to load appropriately sized images and create pixel-perfect layouts.
Methods
get()
- Device pixel ratio as a number
1- mdpi Android devices (160 dpi)1.5- hdpi Android devices (240 dpi)2- iPhone 4, 4S, 5, 5c, 5s, 6, 7, 8, SE; xhdpi Android devices (320 dpi)3- iPhone 6 Plus, 7 Plus, 8 Plus, X, XS, XR, 11, 12; xxhdpi Android devices (480 dpi)3.5- Nexus 6, Pixel XL
getFontScale()
- Font scale as a number (defaults to pixel ratio if not set)
- Android: Settings > Display > Font size
- iOS: Settings > Display & Brightness > Text Size
getPixelSizeForLayoutSize()
layoutSize: Size in density-independent pixels
- Size in physical pixels (rounded to nearest integer)
roundToNearestPixel()
layoutSize: Size in density-independent pixels
- Rounded size that maps to whole pixels
Common Use Cases
Loading Appropriately Sized Images
Creating Pixel-Perfect Hairlines
Responsive Font Sizes
Pixel Grid Snapping
React Native automatically rounds pixel values to avoid blurry elements. All layout calculations use arbitrary precision numbers, but when setting native element positions and dimensions, values are rounded relative to the root (not the parent) to avoid accumulating rounding errors. UseroundToNearestPixel() when you need to ensure a calculated value snaps correctly to the pixel grid: