Coil 2.x が rc-01 から kotlin 1.6.10 に対応しました
https://coil-kt.github.io/coil/changelog/#200-rc01-march-2-2022
これを気に Coil を 1.x から 2.x に Update してみたわけですが、多少変更が必要な箇所があったので備忘録を兼ねて記載します
関数名の変更
以下の関数名の変更がなされています。必要に応じて変えましょう。
-
loadAny
->load
-
clear
->dispose
OkHttpのDiskCacheは非推奨に
Coil に DiskCache が実装されたのでこれを使いましょう。
ちなみに以下で指定しているCacheサイズは 1.x のサイズを参考にしています。
ImageLoader.Builder(context)
.diskCache(
DiskCache.Builder()
.directory(context.cacheDir)
.maxSizePercent(0.02)
.minimumMaxSizeBytes(250L * 1024 * 1024)
.minimumMaxSizeBytes(10L * 1024 * 1024)
.build()
)
.build()
Transformation の interface が変更
key が cacheKey に変更され、 transform 関数の引数が変更されました。
BitmapPool が引数からなくなっているので、素直にBitmapを作って書き換えると良いようです。
override suspend fun transform(
pool: coil.bitmap.BitmapPool,
input: Bitmap,
size: Size
): Bitmap {
val output = pool.get(input.width, input.height, input.config)
// change bitmap
return output
}
override suspend fun transform(input: Bitmap, size: Size): Bitmap {
val output = createBitmap(
input.width,
input.height,
input.config ?: Bitmap.Config.ARGB_8888
)
// change bitmap
return output
}
これに伴ってか、 BlurTransformation と GrayscaleTransformation が削除されたようです。
BlurTransformation and GrayscaleTransformation are removed from the library. If you use them, you can copy their code into your project.
1.x の BlurTransformation を Migrate すると以下のようになると思います。