LoginSignup
4
1

More than 1 year has passed since last update.

【Android】DrawableからBitmapに変換

Last updated at Posted at 2022-03-18

png → bitmap

fun getBitmap(@DrawableRes drawableRes: Int) =
 BitmapFactory.decodeResource(context.resources, drawableRes)

xml → bitmap

fun getBitmap(@DrawableRes drawableRes: Int): Bitmap? {
	val drawable = getDrawable(context, drawableRes) ?: return null
	val canvas = Canvas()
	val bitmap = Bitmap.createBitmap(drawable.intrinsicWidth, drawable.intrinsicHeight, Bitmap.Config.ARGB_8888)
	canvas.setBitmap(bitmap)
	drawable.setBounds(0, 0, drawable.intrinsicWidth, drawable.intrinsicHeight)
	drawable.draw(canvas)
	return bitmap
}

参考サイト

4
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
1