Works on this device
React Native Image Density Generator
Enter the size an image should have on screen, in dp, or the size of the image you have and the screen it is made for. You get the pixel size and the file name for each density (name.png, name@2x.png, name@3x.png), the Android folder of each one, and the code to show the image. No image is changed or uploaded.
Enter your values
Check these values
Your values are worked out in this browser. They are not sent to a server.
Your result will show here
Fill in the values, then choose Make the list.
Your result
How it was worked out
How to use the density generator
- Choose From the size on screen (dp) if you know how large the image should look, such as 24 × 24 dp for an icon.
- Or choose From the image you have and enter its size in pixels and the screen it is made for, such as 300 × 180 px for @3x.
- Choose the scales (the usual @1x, @2x and @3x, or every Android density), the file name and the type.
- Export each size from your design tool with the file names in the table, and copy the code into your component.
How the sizes are worked out
- Pixels = size in dp × scale. An icon of 24 dp is 24 px at @1x, 48 px at @2x and 72 px at @3x.
- The way back: size in dp = pixels ÷ scale. A 300 × 180 px image made for @3x is 100 × 60 dp.
- The arithmetic is exact. A size that is not a whole number of pixels (24.5 dp at @1.5x is 36.75 px) is rounded half up and marked, because an image has whole pixels.
- In code, require only
name.png. React Native picks the file whose scale is closest to the screen of the device. On Android its bundler puts each scale into the drawable folder of that density: @1x in drawable-mdpi, @1.5x in drawable-hdpi, @2x in drawable-xhdpi, @3x in drawable-xxhdpi, @4x in drawable-xxxhdpi.
Worked example
An icon of 24 × 24 dp named icon.png: icon.png is 24 × 24 px, icon@2x.png is 48 × 48 px and icon@3x.png is 72 × 72 px. The code is <Image source={require('./img/icon.png')} style={{ width: 24, height: 24 }} />.
A 100 × 50 px image made for @3x is 33.333 × 16.667 dp. Its @2x file would be 66.667 × 33.333 px, so it is rounded to 67 × 33 px and marked. A size that divides by 3, such as 102 × 51 px, gives whole sizes.
Limits you should know
- The tool gives sizes, names and code. It does not make, resize or compress images.
- Making a larger file from a smaller one adds no detail. Start from the largest size you need.
- Images from the network (a URI) are not picked by scale. Give them a width and a height in the style.
- For Flutter asset folders (2.0x, 3.0x) use the Flutter Icon Size Generator.