WebP Images
WebP is an image format that supports lossy and lossless compressions. Furthermore, it allows for transparency and animations.
Support on Android
Android added WebP support in version 4.0 and improved it in 4.2.1:
- 4.0+ (Ice Cream Sandwich) have basic webp support
- 4.2.1+ (Jelly Beam MR1) have support for transparency and lossless WebP
By adding the Fresco webpsupport module, apps can display all kinds of WebP images on all versions of Android:
Configuration | Basic WebP | Lossless or Transparent WebP | Animated WebP |
---|---|---|---|
OS < 4.0 | |||
OS >= 4.0 | ✓ | ||
OS >= 4.2.1 | ✓ | ✓ | |
Any OS + webpsupport | ✓ | ✓ | |
Any OS + animated-webp | ✓ | (✓ if webpsupport or OS >= 4.2.1) | ✓ |
Adding Support for Static WebP images on Older Versions
The only thing you need to do is add the webpsupport
library to your dependencies. This adds support for all types of non-animated WebP images. E.g. you can use it to display transparent WebP images on Gingerbread.
1
2
3
4
dependencies {
// ... your app's other dependencies
implementation 'com.facebook.fresco:webpsupport:3.5.0'
}
Animated WebP
In order to display animated WebP images, you have to add the following dependencies:
1
2
3
4
5
dependencies {
// ... your app's other dependencies
implementation 'com.facebook.fresco:animated-webp:3.5.0'
implementation 'com.facebook.fresco:webpsupport:3.5.0'
}
You can then load the animated WebP images like any other URI. In order to auto-start the animation, you can set setAutoPlayAnimations(true)
on the DraweeController
:
1
2
3
4
5
DraweeController controller = Fresco.newDraweeControllerBuilder()
.setUri("http://example.org/somefolder/animated.webp")
.setAutoPlayAnimations(true)
.build();
mSimpleDraweeView.setController(controller);
Full Sample
For the full sample see the ImageFormatWebpFragment
in the showcase app: ImageFormatWebpFragment.java