Android Question [Solved] Can we modify B4J outline text code for B4A ?

AnandGupta

Expert
Licensed User
Longtime User
There was a B4A post request to display text with outline.
I found one old thread with some code for B4J.

Can we modify it for B4A ?
 
Solution
Here you have a B4A routine using the ABExtDrawing library.
This library exposes the all the Canvas possibilities.
The Android documentation for Canvas and Paint.
The inspiration I found HERE.

B4X:
Sub Globals
    Private pnlDraw As Panel
    Private cvsDraw As Canvas
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("Layout")
   
    cvsDraw.Initialize(pnlDraw)
   
    DrawTextOutlined(cvsDraw, "Test", 20dip, 100dip, 70, Colors.Red, 3dip, Colors.Blue)
End Sub

Private Sub DrawTextOutlined(cvs As Canvas, Text As String, x As Int, y As Int, TextSize As Float, TextColor As Int, StrokeWidth As Int, StrokeColor As Int)
    Private ExtDraw As ABExtDrawing
    Private ExtPaint1, ExtPaint2 As...

AnandGupta

Expert
Licensed User
Longtime User
getGraphicsContext2D and javafx.scene.paint.Color does not exists in Android. I fear the answe is NO.
That's because it is B4J code. Still I have hope as now we are in B4X age :)
May be some mix of canvas etc. will do. I do not have much experience on canvas but experts here have.
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
Here you have a B4A routine using the ABExtDrawing library.
This library exposes the all the Canvas possibilities.
The Android documentation for Canvas and Paint.
The inspiration I found HERE.

B4X:
Sub Globals
    Private pnlDraw As Panel
    Private cvsDraw As Canvas
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("Layout")
   
    cvsDraw.Initialize(pnlDraw)
   
    DrawTextOutlined(cvsDraw, "Test", 20dip, 100dip, 70, Colors.Red, 3dip, Colors.Blue)
End Sub

Private Sub DrawTextOutlined(cvs As Canvas, Text As String, x As Int, y As Int, TextSize As Float, TextColor As Int, StrokeWidth As Int, StrokeColor As Int)
    Private ExtDraw As ABExtDrawing
    Private ExtPaint1, ExtPaint2 As ABPaint

    ExtPaint1.Initialize
    ExtPaint1.SetTextSize(TextSize * xui.Scale) ' in Android Canvas the text size is in pixels, therefore the multiplication by xui.Scale
    ExtPaint1.SetStyle(ExtPaint1.Style_STROKE)
    ExtPaint1.SetStrokeWidth(StrokeWidth * 2) ' multiplication by two the stroke is only outsides.
    ExtPaint1.SetColor(StrokeColor)
    ExtDraw.drawText(cvs, Text, x, y, ExtPaint1)

    ExtPaint2.Initialize
    ExtPaint2.SetTextSize(TextSize * xui.Scale)
    ExtPaint2.SetColor(TextColor)
    ExtDraw.drawText(cvs, Text, x, y, ExtPaint2)
End Sub

And the result:

1655899311742.png
 
Upvote 0
Solution
Top