codeQ Blog

Chào mừng mọi người đã đến với codeQ Blog

Sử dụng ảnh gif trong React Native

Đăng vào ngày
Cover Image for Sử dụng ảnh gif trong React Native

React Native không tích hợp sẵn việc sử dụng ảnh gif nên muốn sử dụng được bạn phải sử dụng Fresco.

Tìm phần dependencies trong file android/app/build.gradle và thêm như sau

dependencies {
  ...
  // add fresco
  implementation 'com.facebook.fresco:fresco:2.5.0'
  // For animated GIF support
  implementation 'com.facebook.fresco:animated-gif:2.5.0'
}

Để thêm các định dạng ảnh khác

  // For WebP support, including animated WebP
  implementation 'com.facebook.fresco:animated-webp:2.5.0'
  implementation 'com.facebook.fresco:webpsupport:2.5.0'

  // For WebP support, without animations
  implementation 'com.facebook.fresco:webpsupport:2.5.0'

Để sử dụng version mới nhất của Fresco các bạn có thể tham khảo tại đây

Để sử dụng bạn chỉ cần dùng Image trong React Native như bình thường

<Image
  source={require('./animation.gif')}
  style={{width: 200, height: 200 }}
/>

Chúc các bạn thành công!!!