Android Question From android.graphics.drawable.Drawable to Bitmap Watch Thread

Star-Dust

Expert
Licensed User
I need to take a background image from a panel and turn it into a Bitmap.
So Panel.Background -> Bitmap

All the methods found in the forum did not work, not even with Canvas.DrawDrawable how could it be done?

I would like to get a function like this:
DrawToBitmap(Panel as Panel) as Bitmap
 

DonManfred

Expert
Licensed User
Longtime User
Try

B4X:
 Dim Obj1, Obj2 As Reflector
  Dim bmp As Bitmap
  Dim C As Canvas
  Obj1.Target = panel1 ' This is the Panel captured....
  Obj1.Target = Obj1.GetField("vg")
  'bmp.InitializeMutable(Activity.Width, Activity.Height)
  bmp.InitializeMutable(Panel1.Width, Panel1.Height)
  C.Initialize2(bmp)
  Dim args(1) As Object
  Dim types(1) As String
  Obj2.Target = C
  Obj2.Target = Obj2.GetField("canvas")
  args(0) = Obj2.Target
  types(0) = "android.graphics.Canvas"
  Obj1.RunMethod4("draw", args, types)
  Dim Out As OutputStream
  Out = File.OpenOutput(File.DirRootExternal, "Aldrete score" & ".png", False)
  bmp.WriteToStream(Out, 100, "PNG")
  Out.Close
 
Upvote 0

Star-Dust

Expert
Licensed User
Thanks to both of you, you solved a puzzle for me :D;)
 
Upvote 0

Star-Dust

Expert
Licensed User
Try

B4X:
 Dim Obj1, Obj2 As Reflector
  Dim bmp As Bitmap
  Dim C As Canvas
  Obj1.Target = panel1 ' This is the Panel captured....
  Obj1.Target = Obj1.GetField("vg")
  'bmp.InitializeMutable(Activity.Width, Activity.Height)
  bmp.InitializeMutable(Panel1.Width, Panel1.Height)
  C.Initialize2(bmp)
  Dim args(1) As Object
  Dim types(1) As String
  Obj2.Target = C
  Obj2.Target = Obj2.GetField("canvas")
  args(0) = Obj2.Target
  types(0) = "android.graphics.Canvas"
  Obj1.RunMethod4("draw", args, types)
  Dim Out As OutputStream
  Out = File.OpenOutput(File.DirRootExternal, "Aldrete score" & ".png", False)
  bmp.WriteToStream(Out, 100, "PNG")
  Out.Close
On this row:
Obj1.Target = Obj1.GetField("vg")
generate this error: :(
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
progresscircle_drawbitmap (B4A line: 185)
Obj1.Target = Obj1.GetField("vg")
java.lang.NoSuchFieldException: No field vg in class Landroid/view/View; (declaration of 'android.view.View' appears in /system/framework/framework.jar:classes2.dex)
at java.lang.Class.getDeclaredField(Native Method)
at anywheresoftware.b4a.agraham.reflection.Reflection.GetField(Reflection.java:316)
at b4a.example.progresscircle._drawbitmap(progresscircle.java:144)
at b4a.example.progresscircle._initialize(progresscircle.java:231)
at b4a.example.main._activity_create(main.java:378)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:186)
at b4a.example.main.afterFirstLayout(main.java:104)
at b4a.example.main.access$000(main.java:17)
at b4a.example.main$WaitForLayout.run(main.java:82)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6682)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)
 
Upvote 0

Star-Dust

Expert
Licensed User
Use Erels suggestion as it it most probably the best choice.
Of course, the Erel code I tried and it works.
I'm working on an exclusive B4A library, I wanted to try yours too.
It's also something that I'm stubborn about, there will be a way ;)
In any case, Thank for the suggestion
 
Upvote 0
Top