Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I draw text on a path in Jetpack Compose?

Is there a way to write text on a custom path with jetpack compose right now?

Here is an example image of what I want to achieve:

Text drawn on a half-circle path

like image 589
fraherm Avatar asked Nov 06 '20 15:11

fraherm


People also ask

How do I put text on a bitmap?

First we read bitmap from resources. This bitmap is immutable, so we create mutable copy. Then we create Paint object and set them text size, color, shadow, etc. Now we are ready to draw!

How do I get TextField value in jetpack compose?

To read value entered in TextField in Android Compose, declare a variable, and assign this variable the value in the TextField whenever there is a change in the value. The same process holds for reading value entered in OutlineTextField composable.

What is LazyColumn in jetpack compose?

A LazyColumn is a vertically scrolling list that only composes and lays out the currently visible items. It's similar to a Recyclerview in the classic Android View system.

How do you make text curve on Android?

Type your text in the text box. Click Curved text on the left tools menu. Arc it as much as you want, or make circle text.

How can I draw text on the jetpack compose canvas?

At the moment, you cannot draw text directly on the Jetpack Compose canvas. To do so, you have to access the native canvas from the Android framework to draw some text on it.

What is ondraw Lambda in jetpack compose?

With Jetpack Compose, there is a Composable that is part of the UI component library called Canvas to unleash the power of drawing in your application. We are going to draw a smiley face with simple shapes (circle, arc and rectangles) to show its capabilities. The onDraw lambda on the Canvas give us access to the DrawScope.

How to draw text on canvas in Android?

To do so, you have to access the native canvas from the Android framework to draw some text on it. On the onDraw lambda of the Canvas component, you can call the function drawIntoCanvas to access the underlying Canvas with nativeCanvas (quite helpful if you can to reuse some draw logic you implemented in previous Android apps).

Is there a drawtext () method in Android?

Sadly, though, there is no drawText (). That's too bad. But wait, Android's native canvas class ( android.graphics.Canvas) does have several drawText () versions, one of the simplest ones being drawText (String text, float x, float y, Paint paint). Let's see if we can utilize this.


1 Answers

We use nativeCanvas to draw a text using Path in Compose, like how we do normally in Custom Views.

Ex:

@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)
        }
    }
}

Note: we should use android.graphics.Path

And the result will be like:

enter image description here

like image 136
Muthukrishnan Rajendran Avatar answered Oct 13 '22 03:10

Muthukrishnan Rajendran