如何将ColorDrawable转换为位图?

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

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

这是我的ColorDrawable

val blackColorDrawable = ColorDrawable(ContextCompat.getColor(this, R.color.colorPrimaryDark))
android android-drawable android-bitmap
1个回答
1
投票

这是我如何将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
}
© www.soinside.com 2019 - 2024. All rights reserved.