Android Question How to Add Stroke or Shadow to Text

xulihang

Well-Known Member
Licensed User
Longtime User
In B4J, we can apply CSS to add dropShadow effects to text. It even works for text which has already become pixels in an image.

B4X:
Sub setShadow(rgba As String,shadowRadius As String)
    Dim style As String="-fx-effect: dropshadow( three-pass-box , rgba("&rgba&") , "&shadowRadius&" , 1 , 0 , 0 );"
    mStyle = style
End Sub

Are there similar methods in B4A?
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Yes

1761544391863.png

Luca was faster than me...

B4X:
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("MainPage")
    SetShadow(Label1, 1dip, 1dip, 1dip, xui.Color_Gray)
End Sub

Private Sub SetShadow(View As B4XView, Radius As Float, dx As Float, dy As Float, Color As Int)
    View.As(JavaObject).RunMethod("setShadowLayer", Array(Radius, dx, dy, Color))
End Sub
 
Upvote 0

xulihang

Well-Known Member
Licensed User
Longtime User
B4X:
Private Sub SetShadow(View As B4XView, Radius As Float, dx As Float, dy As Float, Color As Int)
    View.As(JavaObject).RunMethod("setShadowLayer", Array(Radius, dx, dy, Color))
End Sub
This works for Label. But if the text is drawn with the BCTextEngine as images, it does not work. Maybe I need to modify the canvas's drawText method to support stroke.
 
Upvote 0

xulihang

Well-Known Member
Licensed User
Longtime User
I created the following code to draw stroked text with B4XCanvas.

B4X:
Private Sub DrawTextWithStroke(text As String,x As Float,y As Float,f As B4XFont,color As Int,strokeColor As Int,alignment As Object,stroke As Float)
    Dim B4XCVSWrapper As JavaObject = cvs
    Dim cvsWrapper As JavaObject = B4XCVSWrapper.GetField("cvs")
    Dim nativeCVS As JavaObject = cvsWrapper.GetField("canvas")
    'Dim nativePaint As JavaObject = cvsWrapper.GetField("paint")
    
    cvsWrapper.RunMethod("checkAndSetTransparent",Array(color))
    
    Dim ctx As JavaObject
    ctx.InitializeContext
    Dim scale As Float =  ctx.RunMethodJO("getResources",Null).RunMethodJO("getDisplayMetrics",Null).GetField("scaledDensity")
    Dim size As Float = f.Size * scale

    Dim Style As EnumClass
    Style.Initialize("android.graphics.Paint.Style")
    
    Dim fillPaint As JavaObject
    fillPaint.InitializeNewInstance("android.graphics.Paint", Null)
    fillPaint.RunMethod("setAntiAlias", Array(True))
    fillPaint.RunMethod("setTextAlign", Array(alignment))
    fillPaint.RunMethod("setTextSize", Array(size))
    fillPaint.RunMethod("setTypeface", Array(f.ToNativeFont))
    fillPaint.RunMethod("setColor", Array(color))
    fillPaint.RunMethod("setStyle", Array(Style.ValueOf("FILL")))
    
    'Dim aa As Boolean = nativePaint.RunMethod("isAntiAlias",Null)
    
    Dim strokePaint As JavaObject
    strokePaint.InitializeNewInstance("android.graphics.Paint", Null)
    strokePaint.RunMethod("setAntiAlias", Array(True))
    strokePaint.RunMethod("setTextAlign", Array(alignment))
    strokePaint.RunMethod("setTextSize", Array(size))
    strokePaint.RunMethod("setTypeface", Array(f.ToNativeFont))
    strokePaint.RunMethod("setColor", Array(strokeColor))
    strokePaint.RunMethod("setStrokeWidth", Array(stroke))
    strokePaint.RunMethod("setStyle", Array(Style.ValueOf("STROKE")))

    nativeCVS.RunMethod("drawText", Array(text, x, y, strokePaint))
    nativeCVS.RunMethod("drawText", Array(text, x, y, fillPaint))
    
    'nativePaint.RunMethod("setAntiAlias",Array(aa))
End Sub
 
Upvote 0

xulihang

Well-Known Member
Licensed User
Longtime User
1761551147382.png


Usage:

DrawTextWithStroke(c, leftOffset, BaseLine, FontMetrics.Fnt, xui.Color_Black, xui.Color_Yellow, "LEFT", 10)
 
Upvote 0
Top