I have a circle in the custom view, and I need to add a shadow. View is always a fixed size, and the circle should be drawn to that size. I added a shadowlayer. However, the value added to the shadowlayer is truncated. I am wondering if there is a way to draw a circle to a given view size and add a shadow.
CODE
class MockupOval(context: Context) : ConstraintLayout(context){
private val paint: Paint
private val rect = RectF(0f, 0f, 0f, 0f)
init {
setWillNotDraw(false)
paint = Paint().apply {
color = ContextCompat.getColor(context,
R.color.mockup_rectangle_default_bg)
style = Paint.Style.FILL
isAntiAlias = true
setShadowLayer(10f, 10f, 10f, 0x80000000.toInt())
setLayerType(View.LAYER_TYPE_SOFTWARE, this)
}
}
override fun onLayout(changed: Boolean, left: Int, top: Int, right:
Int, bottom: Int) {
super.onLayout(changed, left, top, right, bottom)
rect.set(0.0f, 0.0f, width.toFloat(), height.toFloat())
invalidate()
}
override fun onDraw(canvas: Canvas?) {
super.onDraw(canvas)
canvas?.run {
drawOval(rect, paint)
}
}
}