如何将 ColorDrawable 转换为位图?

How to convert ColorDrawable into Bitmap?

我需要将纯色 Drawable 转换为 Bitmap,因为我需要在库中使用 Bitmap。怎么做?

这是我的 ColorDrawable:

val blackColorDrawable = ColorDrawable(ContextCompat.getColor(this, R.color.colorPrimaryDark))

这就是我将 colorDrawable 转换为 bitmap

的方式
try {
      val bitmap = Bitmap.createBitmap(2, 2, Bitmap.Config.ARGB_8888)

      val canvas = Canvas(bitmap)
      blackColorDrawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight())
      blackColorDrawable.draw(canvas)
      return bitmap
} catch (e: Exception) {
      e.printStackTrace()
      return null
}