Share My Creation Simple Rotating Lines

B4X:
Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'These variables can be accessed from all modules.
   Dim Timer1 As Timer
End Sub

Sub Globals
   Dim Canvas1 As Canvas
   Dim r, d, x1, x2, y1, y2  As Double
    Dim dia As Double 
   Dim addx As Double
    dia = cPI * 2
   addx = 0.016
   xcenter=200
   ycenter=400
End Sub

Sub Activity_Create(FirstTime As Boolean)
   Activity.Title = "Simple 3D Rotating lines"
   If FirstTime Then Timer1.initialize("Timer1", 1)
   canvas1.Initialize(Activity)
   Timer1.Enabled = True
End Sub

Sub Activity_Resume
   Timer1_Tick
End Sub

Sub Activity_Pause (UserClosed As Boolean)
   Timer1.Enabled = False
End Sub

Sub Timer1_Tick
    r=r+addx
   If r>dia Then r=0
   Globe
End Sub

Sub Globe
  Canvas1.DrawColor(Colors.Black)
  For d = 0 To 29
       x1 =  Cos(r+(d*addx*10))*100+xcenter
       y1 =  Sin(r+(d*addx*10))*20+ycenter-100
       x2 =  Cos(r+(dia/2)+(d*addx*10))*100+xcenter
       y2 =  Sin(r+(dia/2)+(d*addx*10))*20+ycenter+100
     Canvas1.DrawLine(x1, y1, x2, y2, Colors.RGB(255-(d*4),0,0),1)
  Next
  Activity.Invalidate
End Sub

This is a small graphic demo about rotating lines. It works at average speed on my ZTE Blade.
 

kickaha

Well-Known Member
Licensed User
Longtime User
Very nice, but your resume should be
B4X:
Sub Activity_Resume
   Timer1.Enabled = True
End Sub
otherwise your animation is frozen on resume.
 

nfordbscndrd

Well-Known Member
Licensed User
Longtime User
This is a good graphics programming example.

To make it center up on any device, you could change

xcenter = 200
ycenter = 400

to

Dim lv AsLayoutValues
lv = GetDeviceLayoutValues
xcenter = lv.Width / 2
ycenter = lv.Height / 2
 
Top