Skip to content

Instantly share code, notes, and snippets.

@soroushLotfi
Created January 6, 2022 21:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save soroushLotfi/fa88e2d465573da6cdc424766ead24aa to your computer and use it in GitHub Desktop.
Save soroushLotfi/fa88e2d465573da6cdc424766ead24aa to your computer and use it in GitHub Desktop.
A background drawbale with rounded corners and shadow effect around it
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