B4J Question [Solved] Custom View not draw correctly when form resize

rraswisak

Active Member
Licensed User
Longtime User
HI All,

I try to create a CustomView with B4XCanvas in B4J but found some thing does not work as expected.

I already set the anchor to automatically resize when form resized.

1595652918882.png


And this is the code in the Custom View class
B4X:
#DesignerProperty: Key: BooleanExample, DisplayName: Show Seconds, FieldType: Boolean, DefaultValue: True
#DesignerProperty: Key: TextColor, DisplayName: Text Color, FieldType: Color, DefaultValue: 0xFFFFFFFF, Description: Text color

Sub Class_Globals
    Private mEventName As String 'ignore
    Private mCallBack As Object 'ignore
    Public mBase As B4XView
    Private xui As XUI 'ignore
    Public Tag As Object
    
    Private cvs As B4XCanvas
End Sub

Public Sub Initialize (Callback As Object, EventName As String)
    mEventName = EventName
    mCallBack = Callback
End Sub

'Base type must be Object
Public Sub DesignerCreateView (Base As Object, Lbl As Label, Props As Map)
    mBase = Base
    Tag = mBase.Tag
    mBase.Tag = Me 
      
    cvs.Initialize(mBase)
End Sub

Private Sub Base_Resize (Width As Double, Height As Double)
    cvs.Resize(Width,Height)
    Draw
End Sub

Private Sub Draw
    cvs.ClearRect(cvs.TargetRect)
    
    Dim border As Int = DipToCurrent(5)
    Dim radius As Int = DipToCurrent(25)
    
    Dim rect As B4XRect
    Dim path As B4XPath
    
    path.InitializeRoundedRect(cvs.TargetRect, radius)
    cvs.ClipPath(path)
    cvs.DrawRect(cvs.TargetRect,xui.Color_Blue, True, border)
    cvs.RemoveClip
    
    rect.Initialize(border,border,cvs.TargetRect.Width - border, cvs.TargetRect.Height - border)
    path.InitializeRoundedRect(rect,radius)
    cvs.ClipPath(path)
    cvs.DrawPath(path,xui.Color_Red,True,0)
    
    cvs.Invalidate
End Sub

When the form resize (expand form width), the custom view doesn't clear previous draw
btn1.gif


Any help please, and thank you for your enligntenment
 

rraswisak

Active Member
Licensed User
Longtime User
Okay, found for solution

Remove clip before clearing
B4X:
Private Sub Draw
    cvs.RemoveClip '<-- remove before draw new paint
    cvs.ClearRect(cvs.TargetRect)
    
    Dim border As Int = DipToCurrent(5)
    Dim radius As Int = DipToCurrent(25)
    
    Dim rect As B4XRect
    Dim path As B4XPath
    
    path.InitializeRoundedRect(cvs.TargetRect, radius)
    cvs.ClipPath(path)
    cvs.DrawRect(cvs.TargetRect,xui.Color_Blue, True, border)
    cvs.RemoveClip
    
    rect.Initialize(border,border,cvs.TargetRect.Width - border, cvs.TargetRect.Height - border)
    path.InitializeRoundedRect(rect,radius)
    cvs.ClipPath(path)
    cvs.DrawPath(path,xui.Color_Red,True,0)
    
    cvs.Invalidate
End Sub
 
Upvote 0
Top