Skip to content

Commit

Permalink
fix: black background behind CustomDrawable
Browse files Browse the repository at this point in the history
  • Loading branch information
RichardLuo0 committed Feb 15, 2025
1 parent 420d128 commit 910e865
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,22 @@ object IconHelper {
) : DrawableWrapper(drawable), Adaptively {
private val paint =
Paint(Paint.ANTI_ALIAS_FLAG or Paint.DITHER_FLAG or Paint.FILTER_BITMAP_FLAG)
private var bitmap: Bitmap? = null

override fun draw(canvas: Canvas) {
drawIcon(canvas, paint, bounds, back, upon, mask) { super.draw(canvas) }
if (
bitmap == null ||
bitmap!!.getWidth() != bounds.width() ||
bitmap!!.getHeight() != bounds.height()
) {
bitmap = Bitmap.createBitmap(bounds.width(), bounds.height(), Bitmap.Config.ARGB_8888)
Canvas(bitmap!!).apply {
translate(-bounds.left.toFloat(), -bounds.top.toFloat())
drawIcon(this, paint, bounds, back, upon, mask) { super.draw(this) }
}
}
paint.xfermode = null
canvas.drawBitmap(bitmap!!, null, bounds, paint)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,17 +47,17 @@ open class UnClipAdaptiveIconDrawable(background: Drawable?, foreground: Drawabl
}

protected fun draw(canvas: Canvas, path: Path) {
val mLayersBitmap = mLayersBitmapF?.getAs<Bitmap>(this) ?: return
val mCanvas = mCanvasF?.getAs<Canvas>(this) ?: return
val mPaint = mPaintF?.getAs<Paint>(this) ?: return
if (mLayersShaderF?.get(this) == null) {
val mLayersShaderF = mLayersShaderF ?: return
if (mLayersShaderF.get(this) == null) {
val mLayersBitmap = mLayersBitmapF?.getAs<Bitmap>(this) ?: return
val mCanvas = mCanvasF?.getAs<Canvas>(this) ?: return
mCanvas.setBitmap(mLayersBitmap)
background?.draw(mCanvas)
foreground?.draw(mCanvas)
BitmapShader(mLayersBitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP).let {
mLayersShaderF?.set(this, it)
mPaint.setShader(it)
}
val shader = BitmapShader(mLayersBitmap, Shader.TileMode.DECAL, Shader.TileMode.DECAL)
mLayersShaderF.set(this, shader)
mPaint.setShader(shader)
}
canvas.apply {
translate(bounds.left.toFloat(), bounds.top.toFloat())
Expand All @@ -66,9 +66,9 @@ open class UnClipAdaptiveIconDrawable(background: Drawable?, foreground: Drawabl
}
}

protected fun drawClip(canvas: Canvas) = super.draw(canvas)
protected fun drawClip(canvas: Canvas) = getMask()?.let { draw(canvas, it) }

protected fun getMask() = mMaskScaleOnly?.getAs<Path>(this)
protected fun getMask() = mMaskScaleOnly?.getAs<Path?>(this)

protected fun getFullBoundsPath() = Path().apply { addRect(getFullBounds(), Path.Direction.CW) }

Expand Down

0 comments on commit 910e865

Please sign in to comment.