Do I need set isAntiAlias when I use Android Compose Canvas?

Viewed 312

I have read the article.

I know that maybe I need to set isAntiAlias=true when I use nativeCanvas just like Code A.

Do I need set isAntiAlias when I use Android Compose Canvas such as Code B ? If so, how can I do it?

Code A

@Composable
fun ArcTextExample() {
    val paint = Paint().asFrameworkPaint()
    Canvas(modifier = Modifier.fillMaxSize()) {
        paint.apply {
            isAntiAlias = true
            textSize = 24f
            typeface = Typeface.create(Typeface.DEFAULT, Typeface.BOLD)
        }

        drawIntoCanvas {
            val path = Path()
            path.addArc(RectF(0f, 100f, 200f, 300f), 270f, 180f)
            it.nativeCanvas.drawTextOnPath("Hello World Example", path, 0f, 0f, paint)
        }
    }
}

Code B

@Composable
fun setCanvas() {
    Canvas(
        modifier = Modifier
            .fillMaxSize()
    ) {
        val canvasWidth = size.width
        val canvasHeight = size.height

        drawLine(
            start = Offset(x = 0f, y = canvasHeight),
            end = Offset(x = canvasWidth, y = 0f),
            color = Color.Blue
        )
    }
}
1 Answers

Usually, isAntiAlias is used when drawing text to the canvas, Because it will smoothen the edges

You have to access the native canvas from the Android framework in order to draw some text, you can call all the methods related to the native Canvas like drawText() or drawVertices()

To apply a style to the text, a Paint object must be used, and we can change isAntiAlias property just like the code A.

And in compose there is no concept of Paint because compose already took care of those and made easy to draw shapes, but for drawing texts you can only do this with help of nativeCanvas and paint where you can declare AntiAlias_Flag to true for smoothening the edges.

Related