B4A Library RotaryKnobView

It wraps this posting. Attached the following:

1. B4A project demonstrating the library
2. B4A library files for RotraryKnobView
3. B4A library files for the StringFunctions library (by @margret)
4. The Java Code

Turn the knob with your finger to adjust the value.

1.png



Sample code:

B4X:
#Region  Project Attributes
    #ApplicationLabel: RotaryKnobView
    #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.

    Private rkv1 As RotaryKnobView
    Private steps As Float = 3.6             'step 3.6 degrees at a time
    Private numberofturns As Int = 2         'final angle will be 720 degrees
    Private maxvalue As Int = 100

    Private iv1 As ImageView
    Private iv2 As ImageView
    Private iv3 As ImageView
   
    Dim mbm(10) As Bitmap
   
    Dim sf As StringFunctions
   
   
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    Activity.LoadLayout("main")
   
    sf.Initialize
   
    mbm(0).Initialize(File.DirAssets, "zero.png")
    mbm(1).Initialize(File.DirAssets, "one.png")
    mbm(2).Initialize(File.DirAssets, "two.png")
    mbm(3).Initialize(File.DirAssets, "three.png")
    mbm(4).Initialize(File.DirAssets, "four.png")
    mbm(5).Initialize(File.DirAssets, "five.png")
    mbm(6).Initialize(File.DirAssets, "six.png")
    mbm(7).Initialize(File.DirAssets, "seven.png")
    mbm(8).Initialize(File.DirAssets, "eight.png")
    mbm(9).Initialize(File.DirAssets, "nine.png")
   
    iv1.Bitmap = mbm(0)
    iv2.Bitmap = mbm(0)
    iv3.Bitmap = mbm(0)
   
    rkv1.adjustmentStep = steps
    rkv1.numberOfTurns = numberofturns

End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub rkv1_value_changed(direction As Int, angle As Float)
   
    'direction: +1 => CW; -1 => ACW
    'angle: max => 360 * numberOfTurns
    Dim value As Int = maxvalue * (angle / (numberofturns * 360))
   
    Dim display As String = value
    display = sf.Trim(display)
    If sf.Len(display) = 1 Then
        display = "00" & display
    End If
   
    If sf.Len(display) = 2 Then
        display = "0" & display
    End If   
   
    If sf.Left(display,1) = "0" Then iv1.Bitmap = mbm(0)
    If sf.Left(display,1) = "1" Then iv1.Bitmap = mbm(1)
    If sf.Left(display,1) = "2" Then iv1.Bitmap = mbm(2)
    If sf.Left(display,1) = "3" Then iv1.Bitmap = mbm(3)
    If sf.Left(display,1) = "4" Then iv1.Bitmap = mbm(4)
    If sf.Left(display,1) = "5" Then iv1.Bitmap = mbm(5)
    If sf.Left(display,1) = "6" Then iv1.Bitmap = mbm(6)
    If sf.Left(display,1) = "7" Then iv1.Bitmap = mbm(7)
    If sf.Left(display,1) = "8" Then iv1.Bitmap = mbm(8)
    If sf.Left(display,1) = "9" Then iv1.Bitmap = mbm(9)   
   
    If sf.mid(display,2,1) = "0" Then iv2.Bitmap = mbm(0)
    If sf.mid(display,2,1) = "1" Then iv2.Bitmap = mbm(1)
    If sf.mid(display,2,1) = "2" Then iv2.Bitmap = mbm(2)
    If sf.mid(display,2,1) = "3" Then iv2.Bitmap = mbm(3)
    If sf.mid(display,2,1) = "4" Then iv2.Bitmap = mbm(4)
    If sf.mid(display,2,1) = "5" Then iv2.Bitmap = mbm(5)
    If sf.mid(display,2,1) = "6" Then iv2.Bitmap = mbm(6)
    If sf.mid(display,2,1) = "7" Then iv2.Bitmap = mbm(7)
    If sf.mid(display,2,1) = "8" Then iv2.Bitmap = mbm(8)
    If sf.mid(display,2,1) = "9" Then iv2.Bitmap = mbm(9)
   
    If sf.mid(display,3,1) = "0" Then iv3.Bitmap = mbm(0)
    If sf.mid(display,3,1) = "1" Then iv3.Bitmap = mbm(1)
    If sf.mid(display,3,1) = "2" Then iv3.Bitmap = mbm(2)
    If sf.mid(display,3,1) = "3" Then iv3.Bitmap = mbm(3)
    If sf.mid(display,3,1) = "4" Then iv3.Bitmap = mbm(4)
    If sf.mid(display,3,1) = "5" Then iv3.Bitmap = mbm(5)
    If sf.mid(display,3,1) = "6" Then iv3.Bitmap = mbm(6)
    If sf.mid(display,3,1) = "7" Then iv3.Bitmap = mbm(7)
    If sf.mid(display,3,1) = "8" Then iv3.Bitmap = mbm(8)
    If sf.mid(display,3,1) = "9" Then iv3.Bitmap = mbm(9)   

       
End Sub

The library:

RotaryKnobView
Author:
Author: Thomas, Wrapped by: Johan Schoeman
Version: 1
  • RotaryKnobView
    Events:
    • value_changed (direction As Int, angle As Float)
    Fields:
    • ba As BA
    Methods:
    • BringToFront
    • DesignerCreateView (base As PanelWrapper, lw As LabelWrapper, props As Map)
    • Initialize (EventName As String)
    • Invalidate
    • Invalidate2 (arg0 As Rect)
    • Invalidate3 (arg0 As Int, arg1 As Int, arg2 As Int, arg3 As Int)
    • IsInitialized As Boolean
    • RemoveView
    • RequestFocus As Boolean
    • SendToBack
    • SetBackgroundImage (arg0 As Bitmap)
    • SetColorAnimated (arg0 As Int, arg1 As Int, arg2 As Int)
    • SetLayout (arg0 As Int, arg1 As Int, arg2 As Int, arg3 As Int)
    • SetLayoutAnimated (arg0 As Int, arg1 As Int, arg2 As Int, arg3 As Int, arg4 As Int)
    • SetVisibleAnimated (arg0 As Int, arg1 As Boolean)
    Properties:
    • Background As Drawable
    • Color As Int [write only]
    • Enabled As Boolean
    • Height As Int
    • Left As Int
    • Parent As Object [read only]
    • Tag As Object
    • Top As Int
    • Visible As Boolean
    • Width As Int
    • adjustmentStep As Float [write only]
    • numberOfTurns As Int [write only]
 

Attachments

  • RotaryKnobViewLibFiles.zip
    7.1 KB · Views: 608
  • b4aRotaryKnobView.zip
    107.2 KB · Views: 588
  • TheJavaCode.zip
    12.5 KB · Views: 510
  • StringFunctionsLibFiles.zip
    10.6 KB · Views: 541

Johan Schoeman

Expert
Licensed User
Longtime User

SeaBee

Member
Licensed User
I find this most useful, and propose to make a donation...

...but...

I built a quick test app to see if it would meet my needs, and it did - worked perfectly. I then put the view into the main app, and I could not get the jog.png to show. The RKV works perfectly, but no image, yet in the test app I had no problem once I had put the png file into the visual designer files list.

Any suggestions?
 

Johan Schoeman

Expert
Licensed User
Longtime User
I find this most useful, and propose to make a donation...

...but...

I built a quick test app to see if it would meet my needs, and it did - worked perfectly. I then put the view into the main app, and I could not get the jog.png to show. The RKV works perfectly, but no image, yet in the test app I had no problem once I had put the png file into the visual designer files list.

Any suggestions?
Can you upload a test project so that I can see what the problem is?
 

SeaBee

Member
Licensed User
Can you upload a test project so that I can see what the problem is?
Therein lies the problem - in the test app it works perfectly, but I will build another one to see if it still works or not.

I presume I only need to put the jog.png just into the files folder for this app, and nowhere else?
 

Johan Schoeman

Expert
Licensed User
Longtime User
Therein lies the problem - in the test app it works perfectly, but I will build another one to see if it still works or not.

I presume I only need to put the jog.png just into the files folder for this app, and nowhere else?
Take a look at the original project and see where jog.png is. It is in the /Objects/res/drawable folder of the B4A project - and its property is set to READ ONLY. It is not in the /Files folder...
 
Last edited:

SeaBee

Member
Licensed User
Duh! Now I feel like an idiot. In my test app, I put it in the files folder, where it obviously did nothing, but I built the app in the same folder as the demo app - so it found the jog.png right where it should be.

All fixed now, thanks very much.
 

james_sgp

Active Member
Licensed User
Longtime User
Hi, my B4i app keeps crashing when trying to set Left/Top/Width in a designer script? But works without problem in my B4A app?

James
 

DonManfred

Expert
Licensed User
Longtime User
Hi, my B4i app keeps crashing when trying to set Left/Top/Width in a designer script? But works without problem in my B4A app?
1. B4A project demonstrating the library
2. B4A library files for RotraryKnobView
3. B4A library files for the StringFunctions library (by @margret)
As it is based on JAVA i don´t think it will work in B4i!
 

james_sgp

Active Member
Licensed User
Longtime User
Klaus, removed the library and switched to the class; it has the same problem. I can not add any references to it in Designer Script....
 
Last edited:

james_sgp

Active Member
Licensed User
Longtime User
The library I put in my AdditionalLibrary folder (works no problem on B4A), for my B4i app version I switched to the Class as suggested earlier. But i still have the same issue, I`ll post the project in the morning as its late here.
 
Top