vesp
06/25/2020, 7:42 PMGlide
, 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.Joel Pedraza
06/25/2020, 7:44 PMvesp
06/25/2020, 7:49 PM.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.maskF
and turn it into mask
with this
val mask = Bitmap.createScaledBitmap(gradient, resource.width, scaledHeight, false)
.copy(Bitmap.Config.ARGB_8888, true)
.apply {
setHasAlpha(true)
}
But im not sure if thats readablelet
, also
, apply
, and with
but I'm not sure adding all of that will just muck up the whole thing.Joel Pedraza
06/25/2020, 8:00 PMvesp
06/25/2020, 8:09 PMJoel Pedraza
06/25/2020, 8:11 PMBitmap.createScaledBitmap(gradient, resource.width, scaledHeight, false)
recycled in this version?vesp
06/25/2020, 8:12 PMapply
. Or does splitting it up in another variable allow me to have granular controls of when I should recycleJoel Pedraza
06/25/2020, 8:20 PMit
and this
are from one scope to the othervesp
06/25/2020, 8:32 PMBitmaps
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.Joel Pedraza
06/25/2020, 8:39 PMvesp
06/25/2020, 8:47 PM