1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

読み込み画像のアスペクト比次第でアスペクト比を加工する機能を作る

Posted at

初めに

今回はCoilを使って読み込み画像のアスペクト比次第でアスペクト比を加工していこうと思います

本文

使うのはCoilTransformationです
今回は横長画像の場合は何もせず縦長画像の場合に変換する機能を作ります

class CropTransformation(
    private val aspectRatio: Float = 1.5f,
    override val cacheKey: String,
) : Transformation {

    override suspend fun transform(input: Bitmap, size: Size): Bitmap {
        val inputImageAspectRatio = input.width / input.height
        if (inputImageAspectRatio >= aspectRatio) return input
        val newSize = Size(input.width, (input.width / aspectRatio).roundToInt())
        return Bitmap.createBitmap(
            input,
            0,
            0,
            newSize.width.pxOrElse { 0 },
            newSize.height.pxOrElse { 0 },
        )
    }
}

最後に

やってること自体は画像を再度Bitmapで作るという単純なことですが、備忘録程度に残しておこうと思います

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?