Android Question do the following draw a rectangle with a custom rounded origin???

phongka

Member
I have searched many places but still can't solve this problem. If you know please help me. thank you
01.png




02.png
 

William Lancee

Well-Known Member
Licensed User
Longtime User
I'm not sure if this is what you want, but maybe you could use it as a start.

example.png

B4X:
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Dim w As Float = Root.width
    Dim h As Float = Root.Height
    Dim r As Float = 40
    Dim basePanel As B4XView = xui.CreatePanel("")
    Dim rect As B4XRect
    rect.Initialize(w / 2 - 150, h / 2 - 200, w / 2 + 150, h / 2 + 200)
    Root.AddView(basePanel, rect.Left, rect.top, rect.Width, rect.Height)
    cv.Initialize(basePanel)
    rect.Initialize(0, 0, rect.Width, rect.Height)
    cv.DrawRect(rect, xui.Color_Black, False, 2dip)
    drawArc(0, 0, r, 0, 90, 1, 1)
    drawArc(rect.width, 0, r, 90, 90, -1, 1)
    drawArc(rect.width, rect.height, r, -90, -90, -1, -1)
    drawArc(0, rect.height, r, -90, 90, 1, -1)
    cv.Invalidate
End Sub

Private Sub drawArc(x As Float, y As Float, radius As Float, startSweep As Float, sweep As Float, adjX As Float, adjY As Float)
    Dim path As B4XPath
    path.InitializeArc(x, y, radius, startSweep, sweep)
    cv.DrawPath(path, xui.Color_Black, False, 1dip)
    path.Initialize(x, y)
    path.LineTo(x + adjX * radius, y)
    path.lineTo(x, y + adjY * radius)
    cv.DrawPath(path, xui.Color_Transparent, True, 0)
End Sub
 
Last edited:
Upvote 0

klaus

Expert
Licensed User
Longtime User
You could also do it like this with the ABExtDrawing library.
MyCanvas is the Canvas you want to draw on.

B4X:
DrawRectRoundCorner(MyCanvas, 10dip, 10dip, 130dip, 190dip, 30dip, xui.Color_Red, 2dip, False)

B4X:
Private Sub DrawRectRoundCorner(cvs As Canvas, Left As Int, Top As Int, Right As Int, Bottom As Int, Radius As Int, Color As Int, StrokeWidth As Int, Filled As Boolean)
    Private p As ABPath
    Private r As ABRectF
    Private pa As ABPaint
    Private ABDraw As ABExtDrawing ' you should move this declaration to the Process_Global routine
 
    p.Initialize
    p.moveTo(Left + Radius, Top)
    p.lineTo(Right - Radius, Top)
    r.Initialize(Right - Radius, Top - Radius, Right + Radius, Top + Radius)
    p.arcTo(r, 180, -90)
    p.lineTo(Right, Bottom - Radius)
    r.Initialize(Right - Radius, Bottom - Radius, Right + Radius, Bottom + Radius)
    p.arcTo(r, -90, -90)
    p.lineTo(Left + Radius, Bottom)
    r.Initialize(Left - Radius, Bottom - Radius, Left + Radius, Bottom + Radius)
    p.arcTo(r, 0, -90)
    p.lineTo(Left, Top + Radius)
    r.Initialize(Left - Radius, Top - Radius, Left + Radius, Top + Radius)
    p.addArc(r, 90, -90)
    pa.Initialize
    pa.SetStrokeWidth(StrokeWidth)
    pa.SetColor(Color)
    If Filled = False Then
        pa.SetStyle(pa.Style_STROKE)
    Else
        pa.SetStyle(pa.Style_FILL)
    End If

    ABDraw.drawPath(cvs, p, pa)
End Sub
 
Last edited:
Upvote 0

William Lancee

Well-Known Member
Licensed User
Longtime User
Combining drawing and masking using BitMapCreator

screenShot.png


B4X:
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.Color = xui.color_Red
 
    Dim eraserColor As Int = Root.Color
    Dim w As Float = 300dip
    Dim h As Float = 400dip
    Dim r As Float = 40dip
    Dim rect As B4XRect
    rect.Initialize(0, 0, w, h)
    Dim basePanel As B4XView = xui.CreatePanel("")
 
    Root.AddView(basePanel, Root.Width / 2 - w /2, Root.Height / 2 - h / 2, w, h)
    cv.Initialize(basePanel)
 
    drawArc(0, 0, r, 0, 90)
    drawArc(rect.width, 0, r, 90, 90)
    drawArc(rect.width, rect.height, r, -90, -90)
    drawArc(0, rect.height, r, -90, 90)
 
    Dim mask As BitmapCreator
    mask.Initialize(w, h)
    mask.CopyPixelsFromBitmap(cv.CreateBitmap)
 
    Dim image As BitmapCreator
    image.Initialize(w, h)
    image.CopyPixelsFromBitmap(xui.LoadBitmapResize(File.DirAssets, "picasso.jpg", w, h, True))
    ApplyMaskTo(mask, image, eraserColor)
    cv.DrawBitmap(image.Bitmap, rect)
    cv.Invalidate
End Sub

Private Sub drawArc(x As Float, y As Float, radius As Float, startSweep As Float, sweep As Float)
    Dim path As B4XPath
    path.InitializeArc(x, y, radius, startSweep, sweep)
    cv.DrawPath(path, xui.Color_Black, True, 0)
End Sub

Private Sub ApplyMaskTo(mask As BitmapCreator, image As BitmapCreator, color As Int)
    For i = 0 To mask.mWidth -1
        For j = 0 To mask.mHeight - 1
            If mask.GetColor(i, j) = xui.Color_Black Then image.SetColor(i, j, color)
        Next
    Next
End Sub
 
Last edited:
Upvote 0
Top