The attached program displays the values of the different sensors:
It creates a PhoneSensors object for each type of sensor.
As you can see in the attached code, two Maps are used to hold the phone sensors and the other required data. This way there is no need to write any duplicate code.
Hi Erel, I have an issue....
I force my main Activitie to be portrait, and all the other 4 activities I have in the current project, but only the main one stays in portrait mode when I rotate my device....
__________________
Paulo Gomes - Porto, Portugal - Living/Working in France
Mobile Device: Samsung Galaxy S, Android 2.3.4 CUstom ROM
Laptop: Toshiba NB100-130 (running on Win7Ultimate)
My Posts helped you? Consider Buying me a Porto Glass!
The orientation sensor seems to have an impact on the overall timing of an app. It seems to steal quite a bit of operation time
Could you look at adding options to set the polling rate?
Have a look at this example to see what I mean
Here, a ball is set to roll when the phone is tilted (NOTE: operate in landcape mode). On my Samsung Galaxy S there are notable delays as the ball travels
I have set the timer to 10ms interval in order to allow some leeway for the orientation sensor but there is still a significant impact when polling the sensor
Code:
'AccelBall Sub Process_Globals Dim Orientation AsPhoneOrientation Dim time AsTimer End Sub
Sub Globals Dim can AsCanvas Dim ballx,bally,ballsize AsFloat Dim targety AsFloat End Sub
Sub Activity_Create(FirstTime AsBoolean) ballx=80 : bally=Activity.Height-Activity.Height/2 ballsize=42 : targety=bally can.Initialize(Activity) time.Initialize("Timer1",10) End Sub
Sub Activity_Resume Orientation.StartListening("Orientation") time.Enabled=True End Sub
Sub Activity_Pause (UserClosed AsBoolean) Orientation.StopListening time.Enabled=False End Sub
Sub Timer1_Tick can.DrawColor(Colors.Black) bally=bally+(targety-bally)/2.0 can.DrawLine(ballx,0,ballx,Activity.Height,Colors.Red,1.0) can.DrawCircle(ballx+ballsize,bally,ballsize,Colors.White,True,0.0) Activity.Invalidate End Sub
Sub Orientation_OrientationChanged (Azimuth AsFloat, Pitch AsFloat, Roll AsFloat) targety=targety-Pitch If targety<ballsize Then targety=ballsize If targety>(Activity.Height-ballsize) Then targety=(Activity.Height-ballsize) End Sub
You should not redraw the whole activity each time.
The Accelerometer is more useful for this task.
This code works smoothly: Before running it go to Project - Orientation Supported and choose Landscape only.
Code:
'AccelBall Sub Process_Globals Dim Accelerometer AsPhoneAccelerometer Dim timer1 AsTimer Dim speed AsFloat Dim position AsInt Dim positionY AsInt End Sub
Sub Globals Dim can AsCanvas Dim rect1 AsRect End Sub
Sub Activity_Create(FirstTime AsBoolean) If firstTime Then timer1.Initialize("Timer1",30) EndIf positionY = 100%y - 100dip can.Initialize(Activity) can.DrawLine(0,positionY + 21dip, 100%x, positionY + 21dip,Colors.Red,1.0) position = 50%x rect1.Initialize(0, positionY - 20dip, 40dip, positionY + 20dip) End Sub
Sub Activity_Resume Accelerometer.StartListening("Accelerometer") timer1.Enabled = True End Sub
Sub Activity_Pause (UserClosed AsBoolean) Accelerometer.StopListening timer1.Enabled = False End Sub
Sub Timer1_Tick can.DrawCircle(position, positionY, 20dip, Colors.Black, True, 0) activity.Invalidate2(rect1) position = position + speed If position > 100%x + 20dipThen position = -18dip If position < -20dipThen position = 100%x + 18dip rect1.Left = position - 20dip rect1.Right = position + 40dip can.DrawCircle(position, positionY, 20dip, Colors.White, True, 0) activity.Invalidate2(rect1) End Sub
Sub Accelerometer_AccelerometerChanged (X AsFloat, Y AsFloat, Z AsFloat) speed = speed + Y * 0.5 End Sub
I take it 'dirty rects' is the way forward for updating the screen whilst maintaining relatively smooth performance?
Is this because the draw routines need to be system-friendly (not stealing too much processing power)?
In general, what would be the best solution if you needed to redraw the whole screen, say in a scrolling game environment?
I am guessing this would have to be done with a 2D OpenGL library ?
I'm having trouble running this example. I get this error:
Compiling code. Error
Error parsing program.
Error description: Unknown type: phonesensors
Are you missing a library reference?
Occurred on line: 16
Dim ps As PhoneSensors 'This object is only used to access the type constants.
In the IDE, I have Core, GPS, and Phone(vers 1.31) selected.
Am I missing a reference?
Be nice if there was a "project" option in the IDE to have the references already setup.
The latest Phone library version is 1.32.
Please make sure to copy the new library files into the Libraries folder underneath C:\Program Files\Anywhere Software\Basic4android