Hey everyone I'm playing with `Glide` , `Bitmap`, ...
# android
v
Hey everyone I'm playing with
Glide
,
Bitmap
,
Canvas
, and
Paint
and I managed to create code to apply a mask using
PorterDuff
. Although I managed to complete the task, the Kotlin is kinda unsightly. Can I have some help trying to make function look more like Kotlin? I'm a junior-ish dev so any insights can help. I can DM code or share in comments.
j
Lets see the code 🙂
v
Copy code
.into(object : CustomTarget<Bitmap>(){
    override fun onResourceReady(resource: Bitmap, transition: Transition<in Bitmap>?) {

        val gradient = BitmapFactory.decodeResource(resources, R.drawable.comic_top)
        val resourceAR = resource.width / resource.height.toFloat()
        val scaledHeight = Math.round((resource.width / resourceAR) / 4)
        val maskF = Bitmap.createScaledBitmap(gradient, resource.width, scaledHeight, false)
        val mask = maskF
                .copy(Bitmap.Config.ARGB_8888, true)
                .apply {
                    setHasAlpha(true)
                }


        val result = Bitmap.createBitmap(resource.width, resource.height, Bitmap.Config.ARGB_8888)
        val canvas = Canvas()
        canvas.setBitmap(result)

        val paint = Paint()
        canvas.drawBitmap(resource, 0F, 0F, paint)

        paint.apply {
            isFilterBitmap = true
            xfermode = PorterDuffXfermode(PorterDuff.Mode.DST_OUT)
        }

        canvas.drawBitmap(mask, 0F, (resource.height - mask.height).toFloat(), paint)

        paint.xfermode = null

        container.setImageBitmap(result)
        mask.recycle()
        maskF.recycle()
    }
Here's a portion of it. Apologies but the variables need to be renamed.
I think I can do this with
maskF
and turn it into
mask
with this
Copy code
val mask = Bitmap.createScaledBitmap(gradient, resource.width, scaledHeight, false)
        .copy(Bitmap.Config.ARGB_8888, true)
        .apply { 
            setHasAlpha(true) 
        }
But im not sure if thats readable
I believe theres opportunities to make it more succinct with
let
,
also
,
apply
, and
with
but I'm not sure adding all of that will just muck up the whole thing.
j
@vesp You can add a snippet with syntax highlighting in slack. Use the Shortcuts menu (lighting bolt icon)
v
I didn't know you can do this, @Joel Pedraza! Thanks for the tip heres one more pass with comments. I removed the other glide stuff before it since it didn't seem necessary.
j
Is the intermediate bitmap returned from
Copy code
Bitmap.createScaledBitmap(gradient, resource.width, scaledHeight, false)
recycled in this version?
v
I don't believe so. Is there something that I can do recycle it after a copy is made? Maybe applied after
apply
. Or does splitting it up in another variable allow me to have granular controls of when I should recycle
j
Maybe create a high order function that recycles it
Here’s one way to do it.
👍 1
I dont think gradient is recycled either
Honestly I think it was fine in the first iteration. I didnt have to think about what
it
and
this
are from one scope to the other
v
After your comment, I decided to look at where more of those
Bitmaps
were being made and i
.recycle()
after setting the image. I took care of the
gradient
after auditing. I believe my intent on the second pass was to reduce the amount of variables but I see how all those scopes can just muck it up a bit.
j
I like this one the most, it doesn’t leak all these intermediate variables into the function body’s scope, and doesn’t try to be too clever
v
I think I'll keep that. I can easily comment why I'm doing it this way. Thank you, @Joel Pedraza