FMX.Accelerometer Sample
This sample demonstrates how to use the accelerometer in order to obtain information about the acceleration, angular acceleration, motion and speed.
Contents
Location
You can find the TabSliding sample project at:
- Start | Programs | Embarcadero RAD Studio Athens | Samples and then navigate to:
Object Pascal\Mobile Snippets\Accelerometer
CPP\Mobile Snippets\Accelerometer
- Subversion Repository:
- You can find Delphi and C++ code samples in GitHub Repositories. Search by name into the samples repositories according to your RAD Studio version.
Description
The Accelerometer sample demonstrates how to use the accelerometer in iOS or Android applications. The sample uses TMotionSensor component and TCustomMotionSensor class to offer information about the acceleration, angle, state, and speed of the device motion.
- Note: TCustomMotionSensor class supports Android, iOS and Windows platforms. However, each platform provides different motion sensors and each type of sensor supports a different set of properties (see TCustomMotionSensor for a complete list of platform, sensors and supported properties). Some functionalities of this sample could be disabled depending on the target platform.
How to Use the Sample
- Navigate to one of the locations given above and open:
- Delphi: Accelerometer.dproj.
- C++: Accelerometer.cbproj.
- Select the target platform, iOS and Android supported.
- Press F9 or choose Run > Run.
- Interact with the sample. It shows:
- Acceleration in gals for the X axis.
- Acceleration in gals for the Y axis.
- Acceleration in gals for the Z axis.
- Angular acceleration in degrees per second squared for the X axis.
- Angular acceleration in degrees per second squared for the Y axis.
- Angular acceleration in degrees per second squared for the Z axis.
- Speed in meters per second.
- Specifies whether the device is currently in motion (
True
) or not (False
).
Files
File in Delphi | File in C++ | Contains |
---|---|---|
Accelerometer.dproj |
Accelerometer.cbproj |
The project itself. |
uMain.fmx |
uMain.fmx |
The main form where the components are located. |
uMain.pas |
AccelerometerPCH.h, Accelerometer.cpp |
Used to define and implement the sample. |
Information.txt |
Information.txt |
Further information about the sample. |
Classes
TAccelerometerForm is the main form that represents the main window of the sample. It contains the following components:
- A TListBox object.
- Eight TListBoxItem objects.
- A TSwitch object.
- A TMotionSensor object.
- A TTimer object.
- A ToolBar object with a TLabel object.
Implementation
- The sample uses TMotionSensor to offer information about acceleration, angle, state, and speed of the device motion.
- The sample uses TTimer to repeat specific actions after a given time interval. TTimer specifies the time interval in wich TMotionSensor offers information to the user.
- The sample uses TSwitch to provide the user with a two-way on-off switch. TSwitch on enables TTimer and TMotionSensor.
- The sample uses TListBox to display a set of items of TMotionSensor information in a scrollable list.
When you run the application, TMotionSensor and TTimer are disabled. When you switch on the TSwitch object, TMotionSensor and TTimer are enabled and each TListBoxItem shows different information about the acceleration, angle, state, and speed of the device motion. The sample shows the following information of the device motion:
- The acceleration of the X-axis of the accelerometer.
- The acceleration of the Y-axis of the accelerometer.
- The acceleration of the Z-axis of the accelerometer.
- The angular acceleration of the X-axis of the accelerometer.
- The angular acceleration of the Y-axis of the accelerometer.
- The angular acceleration of the Z-axis of the accelerometer.
- The speed of the accelerometer.
- The motion property of the accelerometer, which could be set to
True
orFalse
.
- Note: Some functionalities of this sample could be disabled depending on the target platform. See TCustomMotionSensor for a complete list of platform, sensors and supported properties.
Uses
See Also
- Mobile Tutorial: Using Location Sensors (iOS and Android)
- Mobile Code Snippets
- TCustomSensor
- TCustomMotionSensor