Android Code Snippet Label fade in-out text change

I needed a way to fade my title label in and out to a new text, quick and dirty, not sure if there is a better way:
ezgif-5-822fb9e313.gif


B4X:
Private Sub animate_label(lbl As B4XView, text As String) As ResumableSub
    Dim animDelay As Int = 200
    Dim startTime As Long = DateTime.Now
    Dim textAlpha As Int = 0
    
    Do While True
        Dim difTime As Long = Max(1, DateTime.Now - startTime)
        textAlpha = Max(0,Abs((difTime / animDelay) * 255 - 255))
        lbl.TextColor = Colors.ARGB(textAlpha, 255,255,255)
        If(textAlpha = 0 Or difTime > animDelay) Then
            Exit
        End If
        Sleep(1)
    Loop
    lbl.Text = text
    startTime = DateTime.Now
    Do While True
        Dim difTime As Long = Max(1, DateTime.Now - startTime)
        textAlpha = Min(255,(difTime / animDelay) * 255)
        lbl.TextColor = Colors.ARGB(textAlpha, 255,255,255)
        If(textAlpha = 255 Or difTime > animDelay) Then
            Exit
        End If
        Sleep(1)
    Loop
End Sub
 
Top