https://kotlinlang.org logo
#android
Title
# android
v

vesp

06/25/2020, 7:42 PM
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

Joel Pedraza

06/25/2020, 7:44 PM
Lets see the code 🙂
v

vesp

06/25/2020, 7:49 PM
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

Joel Pedraza

06/25/2020, 8:00 PM
@vesp You can add a snippet with syntax highlighting in slack. Use the Shortcuts menu (lighting bolt icon)
v

vesp

06/25/2020, 8:09 PM
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

Joel Pedraza

06/25/2020, 8:11 PM
Is the intermediate bitmap returned from
Copy code
Bitmap.createScaledBitmap(gradient, resource.width, scaledHeight, false)
recycled in this version?
v

vesp

06/25/2020, 8:12 PM
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

Joel Pedraza

06/25/2020, 8:20 PM
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

vesp

06/25/2020, 8:32 PM
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

Joel Pedraza

06/25/2020, 8:39 PM
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

vesp

06/25/2020, 8:47 PM
I think I'll keep that. I can easily comment why I'm doing it this way. Thank you, @Joel Pedraza
36 Views