Android Question B4XCanvas.ClearRect - clears to white in B4J but black in B4A!

Brian Dean

Well-Known Member
Licensed User
Longtime User
I am trying to write my first B4X cross-platform (B4J/B4A) application. I am using a class based around a B4XCanvas to draw on a scrollpane/scrollview inner pane/panel passed by the B4J/B4A main form/activity. This all works well until I need to clear the drawing area. In the B4J application the canvas clears and the drawing area is again white and empty as I would expect, but in the B4A activity the drawing area clears to black. Here is a simple project (not the real project) that demonstrates the same behaviour ...

This is the main activity ...
B4X:
Sub Process_Globals
    Private s As sketch
End Sub

Sub Globals
    Private scrMain As ScrollView
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("scroller")
    scrMain.Panel.Width = 100%x
    scrMain.Panel.Height = scrMain.Panel.Width
    If FirstTime Then s.Initialize(scrMain.Panel)
End Sub

Sub Activity_Resume
    s.draw
End Sub

Sub Activity_Pause (UserClosed As Boolean)
End Sub

Here is the class module ...
B4X:
Sub Class_Globals                        ' "sketch" class object
    Dim xui As XUI
    Dim cvs As B4XCanvas
    Dim tmr As Timer
    Dim switch As Boolean = True
End Sub

Public Sub Initialize(v As B4XView)
    cvs.Initialize(v)
    tmr.Initialize("timer", 2000)
End Sub

Sub draw
    Dim r As B4XRect
    r.Initialize(40dip, 40dip, 300dip, 100dip)
    cvs.DrawRect(r, xui.Color_Red, True, 0)           ' The rectangle is drawn even without "cvs.Invalidate" - Why?
    tmr.Enabled = True
End Sub

Sub timer_Tick
    If switch Then draw
    If Not(switch) Then cvs.ClearRect(cvs.TargetRect)
    ' The canvas is cleared to black - Why?    How can I clear it to some other colour?
    cvs.Invalidate                ' This is necessary this time
    switch = Not(switch)
End Sub

Thanks for any pointers. The project is attached.
 

Attachments

  • ClearRect.zip
    9.3 KB · Views: 170

Erel

B4X founder
Staff member
Licensed User
Longtime User
I am trying to write my first B4X cross-platform (B4J/B4A) application. I am using a class based around a B4XCanvas to draw on a scrollpane/scrollview inner pane/panel passed by the B4J/B4A main form/activity.
Use B4XPages.

cvs.ClearRect does clear the canvas. You are seeing the color of the underlying control.
 
Upvote 0

Brian Dean

Well-Known Member
Licensed User
Longtime User
You are seeing the color of the underlying control.

I thought it could be something like that, although when the scrollview initialises the inner panel is white which I hoped would persist.

Use B4XPages.

I did start out as a B4XPages project but then I found that a portrait/landscape design worked better. As this is mainly for experience maybe I will switch back. But now I understand what is happening the solution from @Jeffrey Cameron is also an option
 
Upvote 0
Top