4

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.

enter image description here

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)
        }
    }
}
pistolcaffe
  • 849
  • 1
  • 8
  • 15
  • 1
    See this link https://stackoverflow.com/questions/17655264/how-to-add-a-shadow-and-a-border-on-circular-imageview-android – amit Jun 26 '18 at 02:14
  • thanks. I think reducing the size of the circle and adding a shadow is a way I can try. But I wonder if there is a way to keep the size of the circle and add only the shadow. – pistolcaffe Jun 26 '18 at 02:17

0 Answers0