Sub DrawableToBitmapPureB4A(Drawable As Object) As B4XBitmap
Dim joDrawable As JavaObject = Drawable
' If it is already a BitmapDrawable, extract the Bitmap directly.
Dim className As String = joDrawable.RunMethod("getClass", Null)
If className.Contains("BitmapDrawable") Then
Return joDrawable.RunMethod("getBitmap", Null)
End If
' If it is a VectorDrawable, draw it onto an in-memory canvas
' Returns the drawable's intrinsic size for the current screen density
Dim width As Int = joDrawable.RunMethod("getIntrinsicWidth", Null)
Dim height As Int = joDrawable.RunMethod("getIntrinsicHeight", Null)
' The code only uses the default value (100 pixels) as a safety fallback
' if the loaded resource is a solid color, which mathematically has no dimensions
' (a "borderless color").
If width <= 0 Then width = 100
If height <= 0 Then height = 100
' Initialize the Android classes required to create the Bitmap.
Dim joBitmapClass As JavaObject
joBitmapClass.InitializeStatic("android.graphics.Bitmap")
Dim configClass As JavaObject
configClass.InitializeStatic("android.graphics.Bitmap$Config")
Dim configARGB As Object = configClass.GetField("ARGB_8888")
' Create a mutable Bitmap.
Dim bmp As JavaObject = joBitmapClass.RunMethod("createBitmap", Array(width, height, configARGB))
' Create the Android Canvas associated with that Bitmap.
Dim joCanvas As JavaObject
joCanvas.InitializeNewInstance("android.graphics.Canvas", Array(bmp))
' Draw the Drawable.
joDrawable.RunMethod("setBounds", Array(0, 0, width, height))
joDrawable.RunMethod("draw", Array(joCanvas))
Return bmp
End Sub