Since the b4a community has helped me greatly in the past, I am again giving something back.
For an astronomy app I've build, I needed a nice compass with good performance. As so many of us I started out with canvasses, bitmaps and DrawBitmapRotated. But I soon ran into performance issues on low end devices. So I decided to approach the whole issue in a different way and here is the result.
This compass does not use any bitmap processing or canvasses, instead it uses animations. The attached example project does use two bitmaps, but only to make the compass look good. You could make the whole compass by just using panels for instance.
Here's the code:
So grab it an run with it!
I would really appreciate it if you would post your refined/upgraded/extended versions of this code here in this topic.
That way we can all benefit from our collective effort to build the ultimate b4a compass.
For an astronomy app I've build, I needed a nice compass with good performance. As so many of us I started out with canvasses, bitmaps and DrawBitmapRotated. But I soon ran into performance issues on low end devices. So I decided to approach the whole issue in a different way and here is the result.
This compass does not use any bitmap processing or canvasses, instead it uses animations. The attached example project does use two bitmaps, but only to make the compass look good. You could make the whole compass by just using panels for instance.
Here's the code:
B4X:
Sub Process_Globals
'These global variables will be declared once when the application starts.
'These variables can be accessed from all modules.
Dim Orientation As PhoneOrientation
Dim tmrUpdater As Timer
End Sub
Sub Globals
'These global variables will be redeclared each time the activity is created.
'These variables can only be accessed from this module.
Dim pnlRose As Panel
Dim Rotation As Animation
Dim dblAngle As Double
Dim pnlNeedle As Panel
End Sub
Sub Activity_Create(FirstTime As Boolean)
End Sub
Sub Activity_Resume
dblAngle = 0
Activity.LoadLayout("Compass")
pnlRose.SetBackgroundImage(LoadBitmapSample(File.DirAssets, "compass.png", pnlRose.Width, pnlRose.Width))
pnlNeedle.SetBackgroundImage(LoadBitmapSample(File.DirAssets, "needle.png", pnlRose.Width/9.6, pnlRose.Width))
Rotation.InitializeRotateCenter("Animation", dblAngle, dblAngle, pnlNeedle)
Rotation.Duration = 20000
tmrUpdater.Initialize("tmrUpdater",100)
Orientation.StartListening("Orientation")
tmrUpdater.Enabled = True
End Sub
Sub Activity_Pause (UserClosed As Boolean)
Dim bmpClear As Bitmap
bmpClear.InitializeMutable(1,1)
pnlRose.SetBackgroundImage(bmpClear)
pnlNeedle.SetBackgroundImage(bmpClear)
End Sub
Sub Animation_AnimationEnd
Try
Rotation.Start(pnlNeedle)
Catch
Log(LastException.Message)
End Try
End Sub
Sub Orientation_OrientationChanged (Azimuth As Float, Pitch As Float, Roll As Float)
dblAngle = -Azimuth
End Sub
Sub tmrUpdater_Tick
Rotation.InitializeRotateCenter("Animation", dblAngle, dblAngle, pnlNeedle)
Rotation.Duration = 20000
Rotation.Start(pnlNeedle)
End Sub
So grab it an run with it!
I would really appreciate it if you would post your refined/upgraded/extended versions of this code here in this topic.
That way we can all benefit from our collective effort to build the ultimate b4a compass.