Android Question How to convert TimerTask part from Java to B4A

mcqueccu

Well-Known Member
Licensed User
Longtime User
Dear Frens,
I am trying to learn GestureDetector Library in which i am going to implement OnDoubleTap Gesture to smooth zoom the screen.I have code on Java writtern by my fren but unable to convert it to B4A so if anyone can help me i will be grateful.
Here is the code what i have tried so far :
B4X:
Sub Gesture_onDoubleTap(X As Float, Y As Float, MotionEvent As Object)
'    Log("   onDoubleTap x = " & X & ", y = " & Y & ", ev = " & MotionEvent)
    '    ToastMessageShow("Double-Tap", False)
    Dim initial As Double : initial = zoom
    Dim f As Double : f = 1.5 * zoom
    Dim ctr As Vector2D
    Dim increment As Double
    Dim finalzoom As Double
    If f> 3000 Then f=2999
    finalzoom = f
    increment = (finalzoom - initial)/10
    ctr.Initialize(0,0)
    ctr.Xx = x
    ctr.Yy =y
    Dim v As Double : v= zoom + increment
    If v> finalzoom Then
    Else
        centeredZoom(ctr,zoom+increment)
    End If
End Sub
Original Code on Java :
B4X:
@override
        public boolean onDoubleTap(MotionEvent e)
        {
            //Smooth zoom animation: zoom 1.5X in 200ms
            final float initial = zoom;
            float f = 1.5f * zoom;
            if (f > 3000) f = 2999;
            final float finalzoom = f;
            final float increment = (finalzoom - initial) / 10;

            final Vector2D ctr = new Vector2D(e.getX(), e.getY());

            final Timer rTimer = new Timer();
            TimerTask rTask = new TimerTask()
            {
               @override
                public void run()
                {
                    ((Activity) ctx).runOnUiThread(new Runnable()
                    {
                        @override
                        public void run()
                        {
                            float v = zoom + increment;
                            if (v > finalzoom)
                            {
                                rTimer.cancel();
                            } else
                            {
                                centeredZoom(ctr, zoom + increment);
                            }
                        }
                    });
                }
            };
            rTimer.schedule(rTask, 0, 20);


            return false;
        }


i have no idea how to use this TimerTask and UIthread.
Please help me to Learn.
Thank u.
 

mcqueccu

Well-Known Member
Licensed User
Longtime User
Dear Erel,
Thank u for ur reply.But i am unable to relate the customprogressbar :- AnimateValueTo method to my use.
Should i use the DateTime.Now and loop until the zooming duration.OR just increment the zoom value in loop?
It could be very helpful if elaborated .
Thank u.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
This is the relevant code (download the latest version):
B4X:
Private Sub AnimateValueTo(NewValue As Float)
   Dim n As Long = DateTime.Now
   Dim duration As Int = Abs(currentValue - NewValue) / 100 * DurationFromZeroTo100 + 1000
   Dim start As Float = currentValue
   currentValue = NewValue
   Dim tempValue As Float
   Do While DateTime.Now < n + duration
     tempValue = ValueFromTimeEaseInOut(DateTime.Now - n, start, NewValue - start, duration)
     DrawValue(tempValue)
     Sleep(10)
     If NewValue <> currentValue Then Return 'will happen if another update has started
   Loop
   DrawValue(currentValue)
End Sub

Change DrawValue with a sub that sets the zoom.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…