【发布时间】:2022-11-12 17:33:45
【问题描述】:
我正在使用以下代码将文本绘制到 Canvas 上,但是您从照片中看到的文本正在离开屏幕,而不是遵循我向 Canvas 发出的宽度参数,为什么会发生这种情况?
如果我使用较旧的 android 画布,这似乎不会发生,可能是 compose 错误?
Box(
modifier = Modifier
.fillMaxWidth()
) {
Canvas(
modifier = Modifier.width(500.dp),
onDraw = {
drawIntoCanvas {
it.nativeCanvas.drawText(
text,
0f,
120.dp.toPx(),
textPaintStroke(context = context)
)
it.nativeCanvas.drawText(
text,
0f,
120.dp.toPx(),
textPaint(context = context)
)
}
}
)
}
fun textPaintStroke(
context: Context
): NativePaint {
val customTypeface = ResourcesCompat.getFont(context, R.font.baloo2_semi_bold)
return Paint().asFrameworkPaint().apply {
isAntiAlias = true
style = android.graphics.Paint.Style.STROKE
textSize = 64f
color = android.graphics.Color.BLACK
strokeWidth = 12f
strokeMiter= 10f
strokeJoin = android.graphics.Paint.Join.ROUND
typeface = customTypeface
}
}
fun textPaint(
context: Context
): NativePaint {
val customTypeface = ResourcesCompat.getFont(context, R.font.baloo2_semi_bold)
return Paint().asFrameworkPaint().apply {
isAntiAlias = true
style = android.graphics.Paint.Style.FILL
textSize = 64f
color = android.graphics.Color.WHITE
typeface = customTypeface
}
}
【问题讨论】:
标签: android android-jetpack-compose