Android Question SetBackgroundImage on a Panel with a Canvas

dieterp

Active Member
Licensed User
Longtime User
I have created a panel and set a background image to that panel. I then initialize a canvas to that same panel with the intention of drawing lines on top of the background image of the canvas.

When I try draw the lines though they do not appear on top of the image. It's only once I run Activity.Invalidate that I then see the lines, although that then makes the background image disappear. Anyone know where I am going wrong?
 

dieterp

Active Member
Licensed User
Longtime User
Hi James

I've found a workaround that I picked up in other forum questions (To add a transparent panel over the one I'm using), but I'd still like to know why this happens. Below is the code I am using:

B4X:
Dim PanelImage As Panel
    Dim CanvasCover As Canvas
   
    PanelImage.Initialize("pnl")
    Activity.AddView(PanelImage, 0, 0, 98%x, 98%x)
    PanelImage.Color = Colors.White
   
    PanelImage.SetBackgroundImage(bmpWagonWheel)
   
    CanvasCover.Initialize(PanelImage) 

    CanvasCover.DrawLine(98%x * 0.50, 98%x * 0.33, 35%x, 35%y, Colors.Black, 2dip)
    CanvasCover.DrawLine(98%x * 0.50, 98%x * 0.33, 75%x, 75%y, Colors.Red, 2dip)
    CanvasCover.DrawLine(98%x * 0.50, 98%x * 0.33, 35%x, 90%y, Colors.Blue, 2dip)
   
    'At this point I thought the lines would be drawn on the image but nothing yet
   
    Dim PanelTransparent As Panel
    PanelTransparent.Initialize("pnl")
    Activity.AddView(PanelTransparent, 0, 0, 98%x, 98%x)
   
    'It's only after I add this extra Panel that I see the lines on top of my image


It's only after I add this extra Panel that I see the lines on top of my image. Any idea why this happens?
 
Last edited:
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Dim PanelImage As Panel
Please use code-tags when posting code

codetag002.png
 
Upvote 0

sorex

Expert
Licensed User
Longtime User
that initial code without the additional panel works fine here.

I just added the required lines to get that example working

B4X:
   Dim bmpwagonwheel As Bitmap
   bmpwagonwheel.Initialize(File.DirAssets,"bmp.png")
 
Upvote 0

dieterp

Active Member
Licensed User
Longtime User
Thanks for the feedback guys. I'm not sure why mine doesn't work without adding the extra panel. Will go with the workaround if I can't get it sorted
 
Upvote 0

Cableguy

Expert
Licensed User
Longtime User
Remember that a canvas is like a transparency sheet that you place over a view... You should first draw the canvas before adding it to the view, but if you change it after that, you need to call invalidate methods to refresh the canvas
 
Upvote 0

James Chamblin

Active Member
Licensed User
Longtime User
After drawing on a view, you need to tell the view to update by calling invalidate.
B4X:
Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
    Dim PanelImage As Panel
    Dim CanvasCover As Canvas
    Dim bmpWagonWheel As Bitmap

End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    'Activity.LoadLayout("Layout1")
    bmpWagonWheel = LoadBitmapSample(File.DirAssets,"dscn0536.jpg",100%x,100%y)
    PanelImage.Initialize("pnl")
    Activity.AddView(PanelImage, 0, 0, 98%x, 98%x)
    PanelImage.Color = Colors.White
  
    PanelImage.SetBackgroundImage(bmpWagonWheel)
  
    CanvasCover.Initialize(PanelImage)

    CanvasCover.DrawLine(98%x * 0.50, 98%x * 0.33, 35%x, 35%y, Colors.Black, 2dip)
    CanvasCover.DrawLine(98%x * 0.50, 98%x * 0.33, 75%x, 75%y, Colors.Red, 2dip)
    CanvasCover.DrawLine(98%x * 0.50, 98%x * 0.33, 35%x, 90%y, Colors.Blue, 2dip)
  
    'At this point the lines will not show up.  You need to tell PanelImage to update itself
    PanelImage.Invalidate
  
  
    'It's only after the Invalidate that the extra lines will be seen'
End Sub
 
Upvote 0
Top