override fun onResourceReady(resource: Bitmap, transition: Transition?) { // Grabs the mask without any scaling. Does it need be recycled in the end? val gradient = BitmapFactory.decodeResource(resources, R.drawable.comics_bg_top) // Used to determine the aspect ratio so its sized evenly to bitmap from url. val resourceAR = resource.width / resource.height.toFloat() //Sized in respect to height val scaledHeight = Math.round((resource.width / resourceAR) / 4) //Scaling the bitmap. Not sure if apply is necessary. val mask = Bitmap.createScaledBitmap(gradient, resource.width, scaledHeight, false) .copy(Bitmap.Config.ARGB_8888, true) .apply { setHasAlpha(true) } // result is the blank bitmap that will be placed in imageview val result = Bitmap.createBitmap(resource.width, resource.height, Bitmap.Config.ARGB_8888) val canvas = Canvas() canvas.setBitmap(result) // I think this is where it all kinda falls apart. 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() }