Sub Globals
'These global variables will be redeclared each time the activity is created.
'These variables can only be accessed from this module.
Dim bp As BitmapPlus
Dim bmp, bmpPressed As Bitmap
Dim ivRotatKnob As ImageView
Dim MinAngle As Int
MinAngle = -360
Dim MaxAngle As Int
MaxAngle = 360
Dim Angle, OldAngle As Int
End Sub
Sub Activity_Create(FirstTime As Boolean)
'Activity.Color = Colors.Blue
bmp = LoadBitmap(File.DirAssets, "cat.png")
bmpPressed = LoadBitmap(File.DirAssets, "cat.png")
ivRotatKnob.Initialize("")
ivRotatKnob.Gravity = Gravity.NO_GRAVITY
Activity.AddView(ivRotatKnob, 60dip, 34dip, bmp.Width * Density, bmp.Height * Density)
Dim r As Reflector
r.Target = ivRotatKnob
r.SetOnTouchListener("Knob_Touch")
'Angle = MinAngle
ivRotatKnob.SetBackgroundImage(bp.Rotate(bmp, bmp.Width, bmp.Height, Angle, True))
End Sub
Private Sub getAngle(X As Float, Y As Float) As Float
Dim dx, dy As Float
dx = (ivRotatKnob.Width / 2) - X
dy = Y - (ivRotatKnob.Height / 2)
Return ATan2(-dx, -dy) / cPI * 180
End Sub
Sub CalcAngle(X As Float, Y As Float)
Dim NewAngle As Float
NewAngle = getAngle(X, Y)
Dim Delta, Delta1, Delta2, Delta3 As Float
Delta1 = NewAngle - OldAngle
Delta2 = NewAngle - OldAngle + 360
Delta3 = NewAngle - OldAngle - 360
' What's the shortest distance between the old and new angles ?
If Abs(Delta1) < Abs(Delta2) AND Abs(Delta1) < Abs(Delta3) Then
Delta = Delta1
Else If Abs(Delta2) < Abs(Delta3) Then
Delta = Delta2
Else
Delta = Delta3
End If
Angle = Max(MinAngle, Min(Angle + Delta, MaxAngle))
OldAngle = NewAngle
End Sub
Sub Knob_Touch(ViewTag As Object, Action As Int, X As Float, Y As Float, MotionEvent As Object) As Boolean
Select Action
Case 0 ' DOWN
OldAngle = getAngle(X, Y)
ivRotatKnob.SetBackgroundImage(bp.Rotate(bmpPressed, bmpPressed.Width, bmpPressed.Height, Angle, True))
Case 1 ' UP
ivRotatKnob.SetBackgroundImage(bp.Rotate(bmpPressed,bmpPressed.Width, bmpPressed.Height, Angle, True))
Case 2 ' MOVE
CalcAngle(X, Y)
ivRotatKnob.SetBackgroundImage(bp.Rotate(bmpPressed, bmpPressed.Width, bmpPressed.Height, Angle, True))
End Select
Return True
End Sub