我知道如何通过代码(http://www.androidhive.info/2017/02/android-working-svg-vector-drawables/)使用ImageView显示矢量drawable ,在几篇文章中有描述,但是:
如何使用我的自定义视图和使用画布的代码绘制矢量? 这可能吗?如果是这样,有人可以给我一个暗示。
答案 0 :(得分:0)
In onDraw method
canvas?.drawBitmap(getVectorBitmap(context, R.drawable.ic_icon), 500f, 500f, canvasPaint)
create below method in class
private fun getVectorBitmap(context: Context, drawableId: Int): Bitmap? {
var bitmap: Bitmap? = null
when (val drawable = ContextCompat.getDrawable(context, drawableId)) {
is BitmapDrawable -> {
bitmap = drawable.bitmap
}
is VectorDrawable -> {
bitmap = Bitmap.createBitmap(
drawable.intrinsicWidth,
drawable.intrinsicHeight, Bitmap.Config.ARGB_8888
)
val canvas = Canvas(bitmap)
drawable.setBounds(0, 0, canvas.width, canvas.height)
drawable.draw(canvas)
}
}
return bitmap
}