Help us understand the problem. What is going on with this article?

Android P の ImageDecoder を触ってみる

More than 1 year has passed since last update.

Android P PreviewからImageDecoderが使えるように。

Android P gives you an easier way to decode images to bitmaps or drawables -- ImageDecoder, which deprecates BitmapFactory.

画像の表示

val uri = Uri.parse("android.resource://${packageName}/drawable/image")
val source = ImageDecoder.createSource(contentResolver, uri)
val bitmap = ImageDecoder.decodeBitmap(source)
imageView.setImageBitmap(bitmap)

normal.png

drawableをURIにして読み込む。

リサイズ

val uri = Uri.parse("android.resource://${packageName}/drawable/image")
val source = ImageDecoder.createSource(contentResolver, uri)
val bitmap = ImageDecoder.decodeBitmap(source, { decoder, info, source ->
    decoder.setResize(300, 300)
})
imageView.setImageBitmap(bitmap)

decode...にコールバックを指定すれば、decoderからリサイズなどができる。

resize.png

加工

val uri = Uri.parse("android.resource://${packageName}/drawable/image")
val source = ImageDecoder.createSource(contentResolver, uri)
val bitmap = ImageDecoder.decodeBitmap(source, { decoder, info, source ->
    decoder.setResize(300, 300)
    decoder.setPostProcessor { canvas ->
        val path = Path()
        path.fillType = Path.FillType.INVERSE_EVEN_ODD
        path.addCircle((canvas.width/2).toFloat(), (canvas.height/2).toFloat(), (canvas.width/2).toFloat(), Path.Direction.CW)

        val paint = Paint()
        paint.isAntiAlias = true
        paint.color = Color.TRANSPARENT
        paint.xfermode = PorterDuffXfermode(PorterDuff.Mode.SRC)

        canvas.drawPath(path, paint)

        return@setPostProcessor PixelFormat.TRANSLUCENT
    }
})
imageView.setImageBitmap(bitmap)

setPostProcessorで読み込んだ画像の加工ができる。

preprocess.png

こんな感じに円形にクリッピングなど。


使用した画像

Charlesjsharp / Cassius blue (Leptotes cassius theonus), Cuba / CC BY-SA 4.0
File:Cassius blue (Leptotes cassius theonus) underside.JPG - Wikimedia Commons

tkt989
AndroidやiOSでアプリを作っています 仕事は加えてWeb系もしてます
http://profile.tkt989.info
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした