Android Question Compass object: draw it or import image?

rfresh

Well-Known Member
Licensed User
Longtime User
I'm new to A4B and am interested in building an app that has a compass on the main screen. My question is: do I have to programatically draw the compass or is this something I can import as an image?

I should add I will want to use my finger to turn (rotate) the compass to different headings at the top 12 o'clock position.

Thanks for any help.
 

rfresh

Well-Known Member
Licensed User
Longtime User
I'm still trying to understand how to write the code to make my image rotate. I've been looking at examples but have some questions.

Regarding InitializeRotateCenter(), the docs say this:

Method_636.png
InitializeRotateCenter (EventName As String, FromDegrees As Float, ToDegrees As Float, View As android.view.View)

Similar to InitializeRotate, with the pivot set to the given view's center.

Argument 1: I want to click on a button to rotate my directional gyro 1 degree (out of 360), so I am passing the "btnVORLeft" string which is my Left button object name.
Argument 2: I'm not sure what this means. FromDegrees from what reference point on the compass? If I enter 5 does that mean my directional gyro will move from the 5 degree point on a compass? I have 5 input.
Argument 3: ToDegrees would mean how much I want the image to rotate. So if I want 1 degree per click and I specify FromDegrees as 5 then ToDegrees would be 6? I have 6 input.
Argument 4: I input my panel object (pnlVOR) that holds my directional gyro image.

When I run my code, I click on the Left VOR button and I see my directional gyro compass flicker, and it doesn't do that until I release the mouse button.

The behavior I'm looking for is one button click to rotate the compass 1 degree and stay there. I read somewhere (I can't find it again) that with an image, you can rotate it, but it won't stay rotated. That capability doesn't exist. Or has that been updated? I think I read that in a post that was 2 years old.

I also read that I might need to forget using an image to do this and use a drawing bit map to move the compass and have it remain at it's new position?

Here is my current B4A code:

B4X:
#Region  Project Attributes
    #ApplicationLabel: Holding Patterns
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: portrait
    #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
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 pnlVOR As Panel
    Dim Rotation As Animation
    Private btnVORLeft As Button
    Private btnVORRight As Button
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("VOR")
    pnlVOR.SetBackgroundImage(LoadBitmapSample(File.DirAssets, "dg6b.jpg", pnlVOR.Width, pnlVOR.Width))
    Rotation.InitializeRotateCenter("btnVORRight", 0, 1, pnlVOR)
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub


Sub btnVORLeft_Click

End Sub

Sub btnVORRight_Click
    Try
        Rotation.Start(pnlVOR)
    Catch
        Log(LastException.Message)
    End Try
End Sub
 
Last edited:
Upvote 0

rfresh

Well-Known Member
Licensed User
Longtime User
Hi Klaus,

No, I had not found that reference before. Thanks for pointing it out to me.

The Rotating Needle example looks exactly like what I need.

Thanks Klaus.
 
Last edited:
Upvote 0

klaus

Expert
Licensed User
Longtime User
Are you confirming that I cannot rotate an image of a compass and have it stay rotated in it's new position?
No, you can rotate any View with the JavaObject library, have a look at Views Utils.
I have never really tried with Animations.

In the Beginner's Guide project I rotate bitmaps.
I either rotate the needle bitmap with a fixed compass image or I rotate the compass bitmap with a fixed needle.
 
Upvote 0

ilan

Expert
Licensed User
Longtime User
Regarding InitializeRotateCenter(), the docs say this:

Method_636.png
InitializeRotateCenter (EventName As String, FromDegrees As Float, ToDegrees As Float, View As android.view.View)

Similar to InitializeRotate, with the pivot set to the given view's center.

you could also draw it with canvas. just add the compass background and draw the Needle according to its angle when rotating the device.

use DrawBitmapRotated for that. i think it is better to draw then rotate views. you can like this draw anything you want on the canvas including the angle with DrawText etc.
 
Upvote 0

rfresh

Well-Known Member
Licensed User
Longtime User
Is there any way to control the direction of the rotation of the DrawBitmapRotated() function? Or does it always rotate CCW?
 
Upvote 0

Harris

Expert
Licensed User
Longtime User
https://www.b4x.com/android/forum/threads/compassview.56575/

This does not draw a "compass" but rather shows the direction (horizontal view) based on the phonesensors (magnetic) or GPS bearing.
It is like the little bubble dongle our dads used to suction cup to his dash so many years ago (when current technology didn't exist).

If the device does not support a magnetic sensor, you can supply the Compass.CompassDegrees using GPS Location.Bearing.
When using GPS to determine your direction - you must be moving...

Sample attached.
 

Attachments

  • gpscompass.zip
    25 KB · Views: 268
Last edited:
Upvote 0

rfresh

Well-Known Member
Licensed User
Longtime User
Using one of the examples I was able to get my app working OK. I'm still trying to find out if I can use the DrawBitmapRotated() function and rotate it CW as well as CCW. I'd like to be able to rotate my compass in both directions. Presently I can only rotate it in the default direction.

In the mean time, I developed on a 4 inch screen. My compass image is 320x320 pixels and looks fine on it. When I load my app on my 7 inch Android tablet, the compass looks too small. Should I try to scale it larger or should I have another larger image for 7 inch tablet?
 
Upvote 0

ilan

Expert
Licensed User
Longtime User
i dont understand what you mean by you cannot rotate CCW??

have a look at this simple example.

EDIT: btw this is not the way i would draw a compass i just made a simple example but if i would do that in b4a i would use Accelerated Surface by @Informatix. it simple to use (like using canvas) and it is hardware accelerated.
 

Attachments

  • compass.jar
    495.2 KB · Views: 266
  • compass.zip
    142.4 KB · Views: 299
Upvote 0

klaus

Expert
Licensed User
Longtime User
My compass image is 320x320 pixels and looks fine on it. When I load my app on my 7 inch Android tablet, the compass looks too small.
You should use a bigger image and load it with LoadBitmapResize.
 
Upvote 0
Top