'Progress Indicator by Bruno Silva - 2015 Ninja Dynamics
'Add this class to your project with the module name "ProgressIndicator".
Private Sub Class_Globals
Private mX As Double
Private mY As Double
Private mRadius As Double
Private pX As Double
Private pY As Double
Private mColor() As Double
Private pColor() As Double
Private rColor() As Double
Private curValue As Double
Private angle As Double
Dim cvs As Canvas
Dim pnl As Panel
End Sub
'Initializes the Progress Indicator on the selected Activity.
'The colors are set in ARGB mode. The size represents the circumference diameter.
'Initialization example: myProgress.Initialize(Activity, 50%x, 50%y, 10%y, Array As Double(255, 32, 32, 32), Array As Double(255, 0, 88, 255), Array As Double(255, 255, 255, 255))
Public Sub Initialize(targetActivity As Activity, posX As Double, posY As Double, size As Double, mainColor() As Double, progressColor() As Double, readyColor() As Double)
pnl.Initialize("pnl")
targetActivity.AddView(pnl, 0, 0, 100%x, 100%y)
mX = posX
mY = posY
mRadius = size / 2
mColor = mainColor
pColor = progressColor
rColor = readyColor
cvs.Initialize(pnl)
SetAntiAlias(cvs)
End Sub
'Displays the visual representation of the current progress.
Sub Update(percentage As Double)
If curValue = 100 AND mColor(0) = 0 AND pColor(0) = 0 AND rColor(0) = 0 Then
Dispose
Return
End If
Dim areaRect As Rect
areaRect.Initialize(0, 0, 100%x, 100%y)
cvs.drawRect(areaRect, Colors.Transparent, True, 0)
curValue = percentage
angle = 360 * (curValue / 100)
If curValue >= 100 Then
mRadius = mRadius * 1.01 'Edit this value to tune the radial explosion speed
mColor(0) = Max(mColor(0) - 8, 0) 'Edit this value to tune the fade-out speed
pColor(0) = Max(pColor(0) - 32, 0) 'Edit this value to tune the fade-out speed
rColor(0) = Max(rColor(0) - 8, 0) 'Edit this value to tune the fade-out speed
End If
For a = 0 To Floor(angle)
pX = mX + (CosD(a) * (mRadius * 1.2)) 'Edit this value to change the outer circle thickness
pY = mY + (SinD(a) * (mRadius * 1.2)) 'Edit this value to change the outer circle thickness
cvs.DrawLine(mX, mY, pX, pY, Colors.ARGB(pColor(0), pColor(1), pColor(2), pColor(3)), 1)
Next
cvs.DrawCircle(mX, mY, mRadius, Colors.ARGB(mColor(0), mColor(1), mColor(2), mColor(3)), True, 0)
cvs.DrawCircle(mX, mY, mRadius, Colors.ARGB((curValue / 100) * rColor(0), rColor(1), rColor(2), rColor(3)), True, 0)
pnl.Invalidate
End Sub
'Removes the view.
'This function Is called automatically once the current progress equals 100%.
Sub Dispose
If pnl.IsInitialized Then pnl.RemoveView
End Sub
'Canvas anti-aliasing java reflection method by LucaMS
'https://www.b4x.com/android/forum/members/lucams.51832/
Private Sub SetAntiAlias (c As Canvas)
Dim r As Reflector
Dim NativeCanvas As Object
r.Target = c
NativeCanvas = r.GetField("canvas")
Dim PaintFlagsDrawFilter As Object
PaintFlagsDrawFilter = r.CreateObject2("android.graphics.PaintFlagsDrawFilter", Array As Object(0, 1), Array As String("java.lang.int", "java.lang.int"))
r.Target = NativeCanvas
r.RunMethod4("setDrawFilter", Array As Object(PaintFlagsDrawFilter), Array As String("android.graphics.DrawFilter"))
End Sub