Android Question Resumable Subs Question - Update label smoothly

tufanv

Expert
Licensed User
Longtime User
Hello,

I can't be sure of stg about this new feature so I want to ask :
I have a timer that downloads data every 2 seconds, on jobdone I change the label according to new data. For example it changes from 3.964 to 3.982 . I don't want to change this system , I just want to implement a visual upgrade , for example I want to change the previous value to new value like an animation : 3.964 to 3.982 with incrementing 0.001 in half a second which means thie difference of 0.018 must be incremented in 500 milisecond so every milisecond I must increment the value 0.018/500 ? 0.000036 .

Is this possible easily with the new resumeable subs ? I cchecked at some examples by Erel,and it seems it is easier than using another timer . What would be your suggestion ?

Thanks
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Here you go:
B4X:
Sub UpdateLabelAnimated(Lbl As Label, ToValue As Double, DurationMs As Int)
   Dim StartValue As Double = Lbl.Text
   Dim StartTime As Long = DateTime.Now
   If IsNumber(Lbl.Tag) = False Then Lbl.Tag = 0
   Lbl.Tag = Lbl.Tag + 1
   Dim Index As Int = Lbl.Tag
   Do While DateTime.Now < StartTime + DurationMs
     Dim currentValue As Double = StartValue + (ToValue - StartValue) * (DateTime.Now - StartTime) / DurationMs
     Lbl.Text = NumberFormat2(currentValue, 0, 3, 3, False)
     Sleep(16)
     'check that a new update hasn't started
     If Index <> Lbl.Tag Then
        Return
     End If
   Loop
   currentValue = ToValue
   Lbl.Text = NumberFormat2(currentValue, 0, 3, 3, False)
End Sub

Note that it uses the Tag property to cancel the current loop if UpdateLabelAnimated was called again (with the same label).
 
Last edited:
Upvote 0

tufanv

Expert
Licensed User
Longtime User
Here you go:
B4X:
Sub UpdateLabelAnimated(Lbl As Label, ToValue As Double, DurationMs As Int)
   Dim StartValue As Double = Lbl.Text
   Dim StartTime As Long = DateTime.Now
   If IsNumber(Label1.Tag) = False Then Label1.Tag = 0
   Label1.Tag = Label1.Tag + 1
   Dim Index As Int = Label1.Tag
   Do While DateTime.Now < StartTime + DurationMs
     Dim currentValue As Double = StartValue + (ToValue - StartValue) * (DateTime.Now - StartTime) / DurationMs
     Lbl.Text = NumberFormat2(currentValue, 0, 3, 3, False)
     Sleep(16)
     'check that a new update hasn't started
     If Index <> Label1.Tag Then Return
   Loop
   currentValue = ToValue
   Lbl.Text = NumberFormat2(currentValue, 0, 3, 3, False)
End Sub

Note that it uses the Tag property to cancel the current loop if UpdateLabelAnimated was called again (with the same label).
I tried it now at the jobdone of the httpjob ( when i get the new value ) and when the updatelabelanimated is executed , everything freezes ( not the app but all the operation )

B4X:
If Job.JobName="getir2" Then
            If Job.Success = True Then

                Dim res As String
                res = Job.GetString
                Log("Response from server: " & res)
                Dim parser As JSONParser
                parser.Initialize(res)
                Dim m As Map
                Dim l As List
                l = parser.Nextarray
      
                For i=0 To l.Size-1
                    m=l.Get(i)
                    If m.Get("sembol") = "USD/TRL" Then
                        ok(0).visible=False
                        If m.Get("last")>usd Then
                            ok(0).Text=""
                            ok(0).TextColor=Colors.green
                            ok(0).SetvisibleAnimated(450,True)
                            fiyat(0).TextColor=Colors.green
                        else if m.Get("last")<usd Then
                            ok(0).Text=""
                            ok(0).TextColor=Colors.red
                            fiyat(0).TextColor=Colors.red
                            ok(0).SetvisibleAnimated(450,True)
                        Else
                            ok(0).Text=""
                            fiyat(0).TextColor=Colors.rgb(255,246,175)
                        End If
          
                      
                        usd=m.Get("last")
'                        fiyat(0).Text=NumberFormat2(usd,0,5,5,False) & "  TRY"
                        UpdateLabelAnimated(fiyat(0),usd,500)
                  
                    End If

this code goes for 17 more symbols like eur/usd , eur/rub etc...


but this freezes the events. timer no longer requests updates with httpjob , and when the first updatelabelanimated is executed , it freezes . with the "price went up" icon ( ok(0) is always visible. ( normally it is visible for 0.7 seconds and goes invisible again )

Did i do stg wrong ?
 
Upvote 0

tufanv

Expert
Licensed User
Longtime User
Hard to say without the full code. Which timers? Try to put a breakpoint and see what happens when the UpdateLabelAnimated is called.
I made a silly mistake, text also included "TRY" near to number so it was invalid double error. Watch the video ,I implemented to USD only at the top. It is a perfect animation. Thanks Erel :



 
Upvote 0

tufanv

Expert
Licensed User
Longtime User
I have only one problem,

It was working good for only one currency but when i implemented it to all currencies like : ( example for the first 3 currencies) , it only makes the animation for the last one .

B4X:
For i=0 To l.Size-1
                    m=l.Get(i)
                    If m.Get("sembol") = "USD/TRL" Then
                        ok(0).visible=False
                        If m.Get("last")>usd Then
                            ok(0).Text=""
                            ok(0).TextColor=Colors.green
                            ok(0).SetvisibleAnimated(450,True)
                            fiyat(0).TextColor=Colors.green
                        else if m.Get("last")<usd Then
                            ok(0).Text=""
                            ok(0).TextColor=Colors.red
                            fiyat(0).TextColor=Colors.red
                            ok(0).SetvisibleAnimated(450,True)
                        Else
                            ok(0).Text=""
                            fiyat(0).TextColor=Colors.rgb(255,246,175)
                        End If
           
                       
                        usd=m.Get("last")
                        If animasyon=False Then
                            fiyat(0).Text=NumberFormat2(usd,0,5,5,False) & "  TRY"
                        Else
                            UpdateLabelAnimated(fiyat(0),usd,500)
                        End If
                   
                    End If
               
                    If m.Get("sembol") = "EUR/TRL" Then
                        If m.Get("last")>eur Then
                            ok(1).Text=""
                            ok(1).TextColor=Colors.green
                            fiyat(1).TextColor=Colors.green
                            ok(1).SetvisibleAnimated(450,True)
                        else if m.Get("last")<eur Then
                            ok(1).Text=""
                            ok(1).TextColor=Colors.red
                            fiyat(1).TextColor=Colors.red
                            ok(1).SetvisibleAnimated(450,True)
                        Else
                            ok(1).Text=""
                            fiyat(1).TextColor=Colors.rgb(255,246,175)
                        End If
                        eur=m.Get("last")
                        If animasyon=False Then
                            fiyat(1).Text=NumberFormat2(eur,0,5,5,False) & "  TRY"
                        Else
                            UpdateLabelAnimated(fiyat(1),eur,500)
                        End If
                    End If
               
                    If m.Get("sembol") = "GBP/TRL" Then
                        If m.Get("last")>gbp Then
                            ok(2).Text=""
                            ok(2).TextColor=Colors.green
                            fiyat(2).TextColor=Colors.green
                            ok(2).SetvisibleAnimated(450,True)
                        else if m.Get("last")<gbp Then
                            ok(2).Text=""
                            ok(2).TextColor=Colors.red
                            fiyat(2).TextColor=Colors.red
                            ok(2).SetvisibleAnimated(450,True)
                        Else
                            ok(2).Text=""
                            fiyat(2).TextColor=Colors.rgb(255,246,175)
                        End If
                        gbp=m.Get("last")
                        If animasyon=False Then
                            fiyat(2).Text=NumberFormat2(gbp,0,5,5,False) & "  TRY"
                        Else
                            UpdateLabelAnimated(fiyat(2),gbp,500)
                        End If
                   
                    End If
 
Upvote 0
Top