Android Question Draw panel views programatically on PDF Canvas

mcvburen

Member
Licensed User
Hi,

I have seen examples in the forum of taking screen shots of panels and saving them to a canvas and bitmap image. My trouble is I am trying to draw views to a PDF canvas (or any canvas) programmatically with no views displayed on a layout at the time. I was able to solve this in B4i using Canvas.DrawView as shown below in a sampling of B4i code. Note I do not want to use the canvas.drawtext method since I need column Labels for alignment, style etc..., that are much easier to work with than raw text alone.

Has anyone been able to draw views on a canvas programmatically without the views being visible at the time? I tried Erel's example sub 'DrawView' but with no luck (blank screen) because I think the view must be showing at the time it is placed on the canvas but I can't be sure. I may be missing a step. Any help in pointing me in the right direction would be greatly appreciated.

*******************************************
** Below is B4i pseudocode, not B4A snippet
*******************************************

B4X:
'**** B4I IOS Snippet ****
Private Sub CreatePDFPage
  CreateMotherPanel
  AddRowsToMother
  SaveMotherToPDFCanvas
End Sub

Private Sub CreateMotherPanel
  MotherPanel.Initialize("")
  MotherPanel.Width = PDFWidth
  MotherPanel.Height= PDFLength
  TopIndex=0
End Sub

'Short Example showing column labels added to panels, panels being added to page as rows
Private Sub AddRowsToMother
  Dim Col1,Col2,Col3,Col4  as Label
  Dim PanelRow1,PanelRow2,PanelRow3,PanelRow4 as Panel
  Col1.Initialize("")
  Col1.text = "Quantity"
  Col2.text = "Description"
  etc,…

  PanelRow1.Initialize("")
  PanelRow1.AddView(Col1, …
  PanelRow1.AddView(Col2, …
  etc,…

  MotherPanel.AddView(panelRow1, …
  MotherPanel.AddView(panelRow2, ...
  etc,...

End Sub

Private Sub SaveMotherToPDFCanvas

'Copy all views (row panels) to the PDF canvas
  Dim PDFCanvas as Canvas
  PDFCanvas.InitializePDF(File.DirLibrary,"Test.pdf",612,792)

  For Each v As View In MotherPanel.GetAllViewsRecursive
   If v Is Panel Then
      Dim Rect As Rect
      Dim pRow As Panel
      pRow=v
      Rect.Initialize(pRow.Left, pRow.Top, pRow.Left+pRow.Width, pRow.Top+pRow.Height)
      PDFCanvas.DrawView(pRow,Rect)
   End If
 Next

 PDFCanvas.NextPDFPage

End Sub

Pardon if any misspellings

Thanks
 
Last edited:

Erel

B4X founder
Staff member
Licensed User
Longtime User
The simplest way to get a bitmap from a view is with XUI library:
B4X:
Dim x As B4XView = YourPanel
Dim bmp As Bitmp = x.Snapshot

It does work with off screen views as demonstrated with this code:
B4X:
Dim p As B4XView = xui.CreatePanel("")
p.SetLayoutAnimated(0, 0, 0, 100dip, 100dip)
p.Color = Colors.Red
Activity.SetBackgroundImage(p.Snapshot)
 
Upvote 0
Top