简体   繁体   中英

Custom View with shadow going out of circle background

I'm trying to create a custom view with shadow text.

在此处输入图片说明

As you can see the shadow is going out of circle. I want shadow to be inside of the circle.

The circle is the background of the view. Which I'm setting from xml

android:background="@drawable/background"

here is my onDraw method

override fun onDraw(canvas: Canvas) {

    paint.color = shadowColor
    for (shadow in shadowList) {
        canvas.drawText(text, width / 2 - textBound.width() / 2 + shadow.dx, height / 2 + textBound.height() / 2 + shadow.dy, paint)
    }

    paint.color = textColor
    canvas.drawText(text, (width / 2 - textBound.width() / 2).toFloat(), (height / 2 + textBound.height() / 2).toFloat(), paint)
}

What I want to achieve is the shadow to be inside of circle.

If the background drawable matches the size of your view. You have to find the radius of the circle in your case, you should take the value of half of the view's width or height. Then rather than drawing all shadows in the shadowList, you should only draw shadows sitting in the circle. In order to calculate it you can use bottom-right corner of your view. The distance of the corner to the center point must be equal or lower than the radius of the circle I mentioned above.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM