Android Question Plotting Line On Canvas with Timer ...Not Working

embedded

Active Member
Licensed User
Longtime User
Try to plot Graph with Line. It works with Button not with timer. Please help. Project code attached in zip file




Auto Graph:
#Region  Project Attributes
    #ApplicationLabel: AutoGraph
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: unspecified
    #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.
    Private xui As XUI
    Dim S_width,S_height As Int
    Dim scale As Double
    Dim H_Grid(9),V_Grid(9),Divider_distance As Int
    Dim marker_position As Int
    Dim x0,y0,x1,y1,pxdist,yData As Int
    Dim angle as Double
End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    Private Panel1 As Panel
    Private canvas1 As B4XCanvas
    Private myrect As B4XRect
    Private Label1 As Label
    Private Label2 As Label
    Private Label3 As Label
    Private Label4 As Label
    Private Label5 As Label
    Private Timer1 As Timer
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("Layout")
    Panel1.Height=Panel1.Width/1.33
    Panel1.Width=95%x
    Panel1.Left=2.5%x
    canvas1.Initialize(Panel1)
    myrect.Initialize(Panel1.Left,Panel1.Top,Panel1.Left+Panel1.Width,Panel1.Top+Panel1.Height)
    S_width=100%x
    S_height=100%y
    scale=10dip/10
    Log("SCREEN_W_H_S=" & S_width &"/" & S_height & "/" & scale)
    Divider_distance=Panel1.Width/10
    For i = 0 To 8
        H_Grid(i)=Divider_distance*(i+1)
        Log("Gridlines=" & H_Grid(i))
        canvas1.DrawLine(H_Grid(i),0,H_Grid(i),Panel1.Height,0x7FFFFFFF,2)
        'canvas1.DrawText(H_Grid(i),H_Grid(i),Panel1.Height-2dip, xui.CreateDefaultFont(10), xui.Color_White, "CENTER")
    Next
    
    Divider_distance=Panel1.Height/10
    For i = 0 To 8
        V_Grid(i)=Divider_distance*(i+1)
        Log("V_Gridlines=" & V_Grid(i))
        canvas1.DrawLine(0,V_Grid(i),Panel1.Width,V_Grid(i),0x7FFFFFFF,2)
    Next
    
    For i=0 To 9 Step 2
        canvas1.DrawText(H_Grid(i),H_Grid(i),Panel1.Height-2dip, xui.CreateDefaultFont(10), xui.Color_White, "CENTER")
    Next
    
    For i=0 To 9 Step 2
        canvas1.DrawText(V_Grid(i),1dip,Panel1.Height-V_Grid(i), xui.CreateDefaultFont(10), xui.Color_White, "LEFT")
    Next
    Timer1.Initialize("Timer1", 100)
    Timer1.Enabled = True
    graph_initial_value
End Sub





Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub Button1_Click
    'yData=Rnd(0,Panel1.Height)
    
    y1=Panel1.Height-yData
    Log(x0&"/"&y0&"/"&"/"&x1&"/"&y1)
    'canvas1.DrawLine(0,Panel1.Height,27dip,Panel1.Height-54,xui.Color_Red,4)
    'canvas1.DrawText("72",72,Panel1.Height-2dip, xui.CreateDefaultFont(10), xui.Color_White, "RIGHT")
    canvas1.DrawLine(x0,y0,x1,y1,xui.Color_Yellow,3)
    x0=x1
    y0=y1
    x1=x1+pxdist
    'Timer1.Enabled=True
End Sub

Private Sub Button2_Click
    canvas1.ClearRect(myrect)
    canvas1.Invalidate
End Sub

Sub graph_initial_value
    pxdist=3
    x0=0
    y0=Panel1.Height
    x1=0
    y1=0
    yData=0
    angle=0
End Sub

Sub Timer1_Tick
    'Handle tick event
    Log("tmr running")
    angle=angle+0.1
    yData=200+(100*Sin(cPI*angle))
    Log("ydata=" & yData)
    y1=Panel1.Height-yData
    Log(x0&"/"&y0&"/"&"/"&x1&"/"&y1)
    'canvas1.DrawLine(0,Panel1.Height,27dip,Panel1.Height-54,xui.Color_Red,4)
    'canvas1.DrawText("72",72,Panel1.Height-2dip, xui.CreateDefaultFont(10), xui.Color_White, "RIGHT")
    canvas1.DrawLine(x0,y0,x1,y1,xui.Color_Yellow,3)
    x0=x1
    y0=y1
    x1=x1+pxdist
    'Button1_Click
End Sub
 

Attachments

  • AutoGraph.zip
    9.9 KB · Views: 211
Top