gpt4 book ai didi

java - 如何在android中使用Drawable绘制笔画?

转载 作者:行者123 更新时间:2023-12-01 16:21:09 25 4
gpt4 key购买 nike

我有一个将资源转换为位图的函数,该函数用于绘制自定义标记图。

我想在资源周围画一条笔画,我从堆栈溢出中得到了一些答案,但到目前为止没有一个有效。

这是我的代码:

fun getBitmapFromVector(
context: Context,
@DrawableRes vectorResourceId: Int,
@ColorInt tintColor: Int
): BitmapDescriptor {
val vectorDrawable = ResourcesCompat.getDrawable(
context.resources, vectorResourceId, null
)
if (vectorDrawable == null) {
Log.e(ContentValues.TAG, "Requested vector resource was not found")
return BitmapDescriptorFactory.defaultMarker()
}
val bitmap = Bitmap.createBitmap(
vectorDrawable.intrinsicWidth,
vectorDrawable.intrinsicHeight, Bitmap.Config.ARGB_8888
)

val paintIcon = Paint()
paintIcon.style = Paint.Style.STROKE
paintIcon.color = Color.BLACK
paintIcon.strokeWidth = 1f

val paintStroke = Paint()
paintStroke.style = Paint.Style.FILL
paintStroke.color = tintColor

val canvas = Canvas(bitmap)
val canvasStroke = Canvas()

canvasStroke.drawBitmap(bitmap,10f,10f,paintStroke)

vectorDrawable.setBounds(0, 0, canvas.width, canvas.height)
DrawableCompat.setTint(vectorDrawable, tintColor)
vectorDrawable.draw(canvas)
vectorDrawable.draw(canvasStroke)

return BitmapDescriptorFactory.fromBitmap(bitmap)
}

最佳答案

val paintIcon = Paint()
paintIcon.style = Paint.Style.FILL // you assigned STROKE here
paintIcon.color = Color.BLACK
paintIcon.strokeWidth = 1f

val paintStroke = Paint()
paintStroke.style = Paint.Style.STROKE // you assigned FILL here
paintStroke.color = tintColor

尝试更改那些已注释的行

关于java - 如何在android中使用Drawable绘制笔画?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62273638/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com