Make fresco easier to use.
dependencies {
// add dependency, please replace x.y.z to the latest version
implementation "com.xueqiu.image:loader:x.y.z"
}
ImageLoader use Fresco & RxJava3, please read their documentation before using it.
Initialize image loader with options in your application.
val imageOptions = ImageLoaderOptions()
.withCacheDir(File(cacheDir, "big"))
.withCacheSize(1024 * 1024 * 100)
.withSmallCacheDir(File(cacheDir, "small"))
.withSmallCacheSize(1024 * 1024 * 5)
.withDecodeFormat(Bitmap.Config.RGB_565)
.withHeader(object : BaseHeaderProcessor() {
override fun setHeaders(url: URL): Map<String, String>? {
// replace to your header
val header = HashMap<String, String>()
header["test-key"] = "test-value"
return header
}
})
.withLoaderInterceptor(object : BaseLoaderInterceptor() {
override fun onIntercept(builder: ImageBuilder): ImageBuilder {
// do something if you need
Log.i("load image type ->", builder.imageType)
return builder
}
})
ImageLoader.init(this, imageOptions)
Create the request with image builder.
val builder = ImageBuilder()
.withNetImage(imageUrl)
.withTransformer(BlurTransformer(2, 5))
.isSmall(false)
Then use image loader to load image.
ImageLoader.loadImage(builder).subscribeWith(object : ImageObserver() {
override fun onSuccess(bitmap: Bitmap) {
// do something
}
override fun onOutOfMemory() {
// do something
}
override fun onLoadError(e: Throwable) {
// do something
}
})
And do not forget to dispose the subscription when you no longer need the callback.
<com.xueqiu.image.loader.view.NetImageView
android:id="@+id/iv_image"
android:layout_width="match_parent"
android:layout_height="200dp" />
mIvImage.loadImage(imageUrl) // image or gif url
If you want to make image zoomable, use image browser view
<com.xueqiu.image.loader.view.ImageBrowserView
android:id="@+id/iv_image"
android:layout_width="match_parent"
android:layout_height="200dp" />
Be careful, if you modify the attributes of view after image loading, you may need to call 'sketch()' method to reload image.
Use transformer to change the image.
withTransformer(BlurTransformer(2, 5))
withTransformer(CircleTransformer())
Or create your own transformer.
class CustomTransformer : BaseTransformer() {
override fun transform(bitmap: Bitmap?) {
// do something
}
override fun getCacheKey(): String {
// make your own cache key
return cacheKey
}
}
For more details, please read the example and source code.