Skip to content

Commit f3f81e3

Browse files
authored
Add documentation for vector drawables feature (#4468)
* Add documentation for vector drawables feature * Remove spaces before newline * Improve documentation with links and split examples * Remove extraneous code comment * Add caveats and creation steps * Move caveats to info block * Remove empty space before newline * Update images.md
1 parent c91d58e commit f3f81e3

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

docs/images.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,38 @@ In some cases you might only want to display an image if it is already in the lo
173173

174174
See [CameraRoll](https://github.com/react-native-community/react-native-cameraroll) for an example of using local resources that are outside of `Images.xcassets`.
175175

176+
### Drawable resources
177+
178+
Android supports loading [drawable resources](https://developer.android.com/guide/topics/resources/drawable-resource) via the `xml` file type. This means you can use [vector drawables](https://developer.android.com/develop/ui/views/graphics/vector-drawable-resources) for rendering icons or [shape drawables](https://developer.android.com/guide/topics/resources/drawable-resource#Shape) for, well, drawing shapes! You can import and use these resource types the same as any other [static resource](#static-image-resources) or [hybrid resource](#images-from-hybrid-apps-resources). You have to specify image dimensions manually.
179+
180+
For static drawables that live alongside your JS code, use the `require` or `import` syntax (both work the same):
181+
182+
```tsx
183+
<Image
184+
source={require('./img/my_icon.xml')}
185+
style={{width: 40, height: 40}}
186+
/>
187+
```
188+
189+
For drawables included in the Android drawable folder (i.e. `res/drawable`), use the resource name without the extension:
190+
191+
```tsx
192+
<Image
193+
source={{uri: 'my_icon'}}
194+
style={{width: 40, height: 40}}
195+
/>
196+
```
197+
198+
The one key difference between drawable resources and other image types is that the asset must be referenced at compile-time of the Android application as Android needs to run the [Android Asset Packaging Tool (AAPT)](https://developer.android.com/tools/aapt2) to package the asset. Binary XML, the file format AAPT creates, cannot be loaded over the network by Metro. If you change the directory or name of an asset, you will need to rebuild the Android application each time.
199+
200+
#### Creating XML drawable resources
201+
202+
Android provides comprehensive documentation on each of the supported drawable resource types in its [Drawable resources](https://developer.android.com/guide/topics/resources/drawable-resource) guide, along with examples of raw XML files. You can utilize tools from Android Studio like the [Vector Asset Studio](https://developer.android.com/studio/write/vector-asset-studio) to create vector drawables from Scalable Vector Graphic (SVG) and Adobe Photoshop Document (PSD) files.
203+
204+
:::info
205+
You should try to avoid referencing other resources in the XML file you create if you want to treat your XML file as a static image resource (i.e. with an `import` or `require` statement). If you wish to utilize references to other drawables or attributes, like [color state lists](https://developer.android.com/guide/topics/resources/color-list-resource) or [dimension resources](https://developer.android.com/guide/topics/resources/more-resources#Dimension), you should include your drawable as a [hybrid resource](#images-from-hybrid-apps-resources) and import it by name.
206+
:::
207+
176208
### Best Camera Roll Image
177209

178210
iOS saves multiple sizes for the same image in your Camera Roll, it is very important to pick the one that's as close as possible for performance reasons. You wouldn't want to use the full quality 3264x2448 image as source when displaying a 200x200 thumbnail. If there's an exact match, React Native will pick it, otherwise it's going to use the first one that's at least 50% bigger in order to avoid blur when resizing from a close size. All of this is done by default so you don't have to worry about writing the tedious (and error prone) code to do it yourself.

0 commit comments

Comments
 (0)