如何在Android中使用Drawable绘制笔画?

问题描述 投票:0回答:1

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

我想在资源周围画一个笔画,我从堆栈溢出中得到了一些答案,但是到目前为止,它们都没有起作用。

这是我的代码:

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)
}
java android kotlin drawable stroke
1个回答
0
投票
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

尝试更改已注释的行

© www.soinside.com 2019 - 2024. All rights reserved.