Build Device Tilt Control Mechanisms With A Motion Sensor In Delphi XE6 & XE7 Firemonkey On Android And IOS

Delphi XE6 & XE7 Firemonkey Accelerometer Sample CodeA motion sensing Accelerometer demo in Delphi XE6 and XE7 Firemonkey allows you to track the tilt of your device. The component that the Accelerometer demo uses is called TMotionSensor. Different devices have different capabilities but the Android and iOS devices that I tested only reported Acceleration X, Acceleration Y, and Acceleration Z. The demo also lists Angle X, Angle Y, Angle Z, Motion, and Speed which it exposes through TMotionSensor.Sensor.AvailableProperties. You can use the X, Y, and Z numbers to build a control mechanism based on the tilt of the device. I added two different pieces of code using the accelerometer numbers to the demo. The first is basically a ball that rolls around on the screen when you tilt the device. The ball is confined within a circle so that it does not disappear off of the screen. You could change the confined space of the ball from the circle to the size of the screen and turn it into a game. The second control detects the tilt of the device and displays a circle which represents a direction (left, right, up, down). You could use this tilt direction detection to fire events within your app (like sliding components on and off the screen or controlling the placement and rotation of an object). The tricky part of the accelerometer is that you have to swap the X and Y depending on if your device is in Landscape or Portrait mode. The code should also work in Appmethod.

Download the modified accelerometer demo and implement tilt controls into your own apps.

Have Delphi Firemonkey questions? Ask and get answers on StackOverflow.

Leave a Reply

Show Buttons
Hide Buttons
Read previous post:
Parent Controls And TEdit TextPrompt Workaround In Delphi XE7 Firemonkey On Android And IOS

There is an issue with the TextPrompt property of TEdit in Delphi XE7 Firemonkey where it does not show up....