Android Question Just for fun (quiz?)

LucaMs

Expert
Licensed User
Longtime User
How to obtain this effect? (the gif animation is not so good, the "real" animation is more fluid)

[I dragged the sun quickly clockwise and released the mouse; with an elastic effect, it returns to its place, slowing down.]
Elastic effect2.gif





I don't need it !!!
(just a curiosity. Can anyone create it with little code? :))
 
Last edited:

eurojam

Well-Known Member
Licensed User
Longtime User
may be it is a trick...getting real complex code for free....:cool: and making millions of dollars...like flappy bird:rolleyes:
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
B4X:
Sub Process_Globals
   Private timer1 As Timer
   Private smiley As BitmapData
   Private boxSize As Int = 50dip
   Private StartTime As Long
   Private Const Duration As Int = 5000
   Private Const NumberOfLoops As Float = 5
   Private smileyX, smileyY As Float
   Private centerX, centerY As Float
   Private radiusStart As Float
   Private angleOffset As Float
End Sub

Sub Globals
   Private gv As GameView
End Sub

Sub Activity_Create(FirstTime As Boolean)
   gv.Initialize("gv")
   If FirstTime Then
     timer1.Initialize("timer1", 16)
     smiley.Bitmap = LoadBitmap(File.DirAssets, "smiley.png")
     centerX = 50%x
     centerY = 50%y
     smileyX = centerX
     smileyY = centerY
     smiley.DestRect.Initialize(0, 0, 0, 0)
   End If
   Activity.AddView(gv, 0, 0, 100%x, 100%y)
   gv.BitmapsData.Add(smiley)
   timer1.Enabled = True
End Sub

Sub gv_Touch (Action As Int, X As Float, Y As Float)
   smileyX = X
   smileyY = Y
   If Action = Activity.ACTION_MOVE Then
     StartTime = 0
   Else If Action = Activity.ACTION_UP Then
     StartTime = DateTime.Now
     Dim dx As Float = smileyX - centerX
     Dim dy As Float = smileyY - centerY
     radiusStart = Sqrt(Power(dx, 2) + Power(dy, 2))
     angleOffset = ATan2(dy, dx)
   End If
End Sub

Sub Timer1_Tick
   If StartTime > 0 And DateTime.Now < StartTime + Duration Then
     Dim t As Float = (DateTime.Now - StartTime) / Duration
     Dim angle As Float = angleOffset + NumberOfLoops * 2 * cPI * t
     Dim radius As Float = radiusStart - radiusStart * t
     smileyX = centerX + radius * Cos(angle)
     smileyY = centerY + radius * Sin(angle)
     smiley.Rotate = angle * 180 / cPI
   End If
   smiley.DestRect.Left = smileyX - boxSize
   smiley.DestRect.Right = smileyX + boxSize
   smiley.DestRect.Top = smileyY - boxSize
   smiley.DestRect.Bottom = smileyY + boxSize
   gv.Invalidate
End Sub

Movement is smooth in release mode.

Project is attached (SpiralSmiley).
 

Attachments

  • SpiralSmiley.zip
    10.1 KB · Views: 227
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Code to create a spring effect (not the same as the effect from the first post):
B4X:
Sub Timer1_Tick
   If StartTime > 0 And DateTime.Now < StartTime + Duration Then
     Dim t As Float = (DateTime.Now - StartTime) / Duration
     Dim radius As Float = radiusStart - radiusStart * t
     Dim k As Float = radius * Cos(50 * t)
     smileyX = centerX + Cos(angleOffset) * k
     smileyY = centerY + Sin(angleOffset) * k
   End If
   smiley.DestRect.Left = smileyX - boxSize
   smiley.DestRect.Right = smileyX + boxSize
   smiley.DestRect.Top = smileyY - boxSize
   smiley.DestRect.Bottom = smileyY + boxSize
   gv.Invalidate
End Sub
Decrease the duration to 2000 or 3000.
 
Upvote 0

AnandGupta

Expert
Licensed User
Longtime User
LucaMs I like your curiosity :)

It got us unexpected and beautiful code from expected master Erel !

Do keep up your curiosity :D

Regards,

Anand
 
Upvote 0
Top