B4A Library RotaryKnobView

Discussion in 'Additional libraries, classes and official updates' started by Johan Schoeman, Apr 10, 2016.

  1. Johan Schoeman

    Johan Schoeman Expert Licensed User

    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.


    Sample code:

    #Region  Project Attributes
    #ApplicationLabel: RotaryKnobView
    #VersionCode: 1
    '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(10As 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:
    0).Initialize(File.DirAssets, "zero.png")
    1).Initialize(File.DirAssets, "one.png")
    2).Initialize(File.DirAssets, "two.png")
    3).Initialize(File.DirAssets, "three.png")
    4).Initialize(File.DirAssets, "four.png")
    5).Initialize(File.DirAssets, "five.png")
    6).Initialize(File.DirAssets, "six.png")
    7).Initialize(File.DirAssets, "seven.png")
    8).Initialize(File.DirAssets, "eight.png")
    9).Initialize(File.DirAssets, "nine.png")
        iv1.Bitmap = mbm(
        iv2.Bitmap = mbm(
        iv3.Bitmap = mbm(
        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:

    Author: Thomas, Wrapped by: Johan Schoeman
    Version: 1
    • RotaryKnobView
      • value_changed (direction As Int, angle As Float)
      • ba As BA
      • 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)
      • 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]

    Attached Files:

  2. Informatix

    Informatix Expert Licensed User

    Johan Schoeman and Erel like this.
  3. Johan Schoeman

    Johan Schoeman Expert Licensed User

  4. Daniel-White

    Daniel-White Active Member Licensed User

    Very good.
    Johan Schoeman likes this.
  5. SeaBee

    SeaBee Member Licensed User

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


    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?
  6. Johan Schoeman

    Johan Schoeman Expert Licensed User

    Can you upload a test project so that I can see what the problem is?
  7. SeaBee

    SeaBee Member Licensed 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?
  8. Johan Schoeman

    Johan Schoeman Expert Licensed User

    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: Jan 12, 2017
  9. SeaBee

    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.
    Johan Schoeman likes this.
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice