This code demonstrates how to draw text along a curved path on an Android Canvas using Kotlin. It achieves this by creating a bitmap, defining a path (a circle in this case), and then using the drawTextOnPath function to place text along that path.
Breakdown:
Setting Up the Canvas:
- The
onCreatemethod of theMainActivityclass inflates the layout (activity_main.xml) and retrieves theImageViewfrom it. - A new
Bitmapwith a specific size and configuration is created usingBitmap.createBitmap. - A
Canvasis then obtained from the bitmap for drawing operations. The canvas background is set to a light blue color usingdrawColor.
- The
Defining the Path and Style:
- A
Paintobject is created to define the style of the path. Anti-aliasing is enabled, the color is set to dark blue, stroke width is set to a thick value, and the paint style is set to stroke (to draw only the outline of the path). - A
Pathobject is created and a circle is added to it usingaddCircle. The circle is centered within the canvas and its radius is calculated to fit within the canvas dimensions with some padding. - The defined path is then drawn onto the canvas using
drawPath.
- A
Drawing Text on the Path:
- A separate
TextPaintobject is created specifically for drawing text. Similar to the path paint, anti-aliasing is enabled, the color is set to white, and the text size is defined. - Finally, the
drawTextOnPathfunction is called. This function takes the text to be drawn, the path it should follow, the starting position along the path, the vertical offset from the path, and theTextPaintobject that defines the text style.
- A separate
Displaying the Result:
- The function
drawTextOnPathreturns the createdBitmapcontaining the drawn path and text. - In the
onCreatemethod, this bitmap is set as the image source for theImageViewretrieved earlier.
- The function
Summary:
This code provides a basic example of how to leverage the drawTextOnPath function in Kotlin to achieve creative text layouts by following a defined path on a Canvas. You can modify the code to experiment with different path shapes, text properties, and positioning to create unique visual effects in your Android applications.
MainActivity.kt
package com.cfsuman.kotlintutorials
import android.app.Activity
import android.graphics.*
import android.os.Bundle
import android.text.TextPaint
import android.widget.ImageView
import kotlin.math.min
class MainActivity : Activity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// get the widgets reference from XML layout
val imageView = findViewById<ImageView>(R.id.imageView)
// show drawing on image view
imageView.setImageBitmap(drawTextOnPath())
}
}
// function to draw text on path on canvas
fun drawTextOnPath():Bitmap?{
val bitmap = Bitmap.createBitmap(
1400,
900,
Bitmap.Config.ARGB_8888
)
// canvas for drawing
val canvas = Canvas(bitmap).apply {
drawColor(Color.parseColor("#A2A2D0"))
}
// paint to draw path
val paint = Paint().apply {
isAntiAlias = true
color = Color.parseColor("#333399")
strokeWidth = 125F
style = Paint.Style.STROKE
}
// add circle to path
val path = Path()
path.addCircle(
canvas.width/2F,
canvas.height/2F,
min(canvas.width,canvas.height)/2 - 100F,
Path.Direction.CCW
)
// draw path on canvas
canvas.drawPath(path, paint)
// text paint to draw text on path
val textPaint = TextPaint().apply {
isAntiAlias = true
color = Color.parseColor("#F0FFFF")
textSize = 80F
}
// finally, draw text on path
canvas.drawTextOnPath(
"Android Development", // text to draw
path, // path the text should follow for its baseline
// distance along the path to add
// to the text's starting position
1250F,
// distance above(-) or below(+) the
// path to position the text
30F,
textPaint // paint
)
return bitmap
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/rootLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#DCDCDC"
android:padding="24dp">
<ImageView
android:id="@+id/imageView"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
- android kotlin - TextView margin programmatically
- android kotlin - TextView add border programmatically
- android kotlin - Coroutines get html from url
- android kotlin - Coroutines JSON from URL
- android kotlin - Coroutines with LiveData
- android kotlin - Coroutines async await all
- android kotlin - Coroutines cancel job
- android kotlin - Coroutines start undispatched
- android kotlin - Coroutines delay
- android kotlin - Canvas draw dotted line
- android kotlin - Canvas draw dashed line
- android kotlin - Canvas draw multiple lines
- android kotlin - Canvas draw line
- android kotlin - Canvas draw arc between two points
- android kotlin - Canvas draw path