Created
January 6, 2022 21:19
-
-
Save soroushLotfi/fa88e2d465573da6cdc424766ead24aa to your computer and use it in GitHub Desktop.
A background drawbale with rounded corners and shadow effect around it
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.graphics.* | |
import android.graphics.drawable.Drawable | |
class BackgroundDrawable : Drawable() { | |
private val shadowSize = 3f.dp | |
private val rectRadius = 5f.dp | |
private val rect = RectF() | |
private val paint = Paint(Paint.ANTI_ALIAS_FLAG).apply { | |
style = Paint.Style.FILL | |
setShadowLayer( | |
shadowSize, | |
0f, | |
0f, | |
Color.LTGRAY | |
) | |
} | |
var backgroundColor = Color.TRANSPARENT | |
set(value) { | |
field = value | |
invalidateSelf() | |
} | |
override fun onBoundsChange(bounds: Rect) { | |
rect.apply { | |
left = shadowSize | |
top = shadowSize | |
right = bounds.right - shadowSize | |
bottom = bounds.bottom - shadowSize | |
} | |
invalidateSelf() | |
} | |
override fun draw(canvas: Canvas) { | |
paint.color = backgroundColor | |
canvas.drawRoundRect( | |
rect, | |
rectRadius, | |
rectRadius, | |
paint | |
) | |
} | |
override fun setAlpha(alpha: Int) { | |
} | |
override fun setColorFilter(colorFilter: ColorFilter?) { | |
} | |
override fun getOpacity(): Int { | |
return PixelFormat.TRANSLUCENT | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment