0

I need to convert solid color Drawable to Bitmap, because I need to use Bitmap in library. How to do that?

This is my ColorDrawable:

val blackColorDrawable = ColorDrawable(ContextCompat.getColor(this, R.color.colorPrimaryDark))
martin1337
  • 2,384
  • 6
  • 38
  • 85
  • Possible duplicate of [How to convert a Drawable to a Bitmap?](https://stackoverflow.com/questions/3035692/how-to-convert-a-drawable-to-a-bitmap) – Son Truong Feb 22 '19 at 08:18

1 Answers1

2

This is how I convert colorDrawable to 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
}
Bach Vu
  • 2,298
  • 1
  • 15
  • 19