This component can be used to obtain data of four types of sensors: acceleration sensor (accelerometer), gravity sensor (gravity), linear acceleration sensor (linearAccelerometer), and gyroscope sensor (gyroscope). The acceleration is the sum of linear acceleration and gravity acceleration. Features such as gravity sensing and shaking are achieved based on the sensors.
The manifest.xml file can contain only one VariableBinders and SensorBinder. You need to select a suitable sensor based on the scenario. For example, accelerometer is recommended for implementing gravity sensing.
<VariableBinders>
<SensorBinder type="" vibrate="" shakeTime="" delay="">
<Variable name="" index=""/>
<Variable name="" index=""/>
<Variable name="" index=""/>
</SensorBinder>
</VariableBinders> Parameter | Type | Mandatory | Description |
|---|---|---|---|
type | String | Yes | Sensor types. The options are accelerometer, gravity, linearAccelerometer, and gyroscope. |
vibrate | Number | No | Indicates whether vibration is triggered by shaking. The options are 0 (no) and 1 (yes). The default value is 0 and expressions are supported. |
shakeTime | Number | No | Duration of vibration triggered by shaking, in milliseconds. The default value is 0 and expressions are supported. |
delay | Number | No | Delay for shaking to trigger vibration, in milliseconds. The default value is 0 and expressions are supported. |
index | Number | Yes | Direction of a sensor. The options are 0, 1, and 2, which are described in the following table. |
name | String | Yes | Name of the variable that records the acceleration or velocity in the direction specified by index. You can obtain the value of this parameter through #name. |
Index values
Sensor Type | Index Value | Description |
|---|---|---|
accelerometer | 0, 1, 2 |
|
gravity | 0, 1, 2 |
|
linearAccelerometer | 0, 1, 2 |
|
gyroscope | 0, 1, 2 |
|
Example 1: Use the obtained gravity acceleration to control the direction and speed of snowfall in <DropPhysicalView>. The positive X-axis direction of the gravity sensor is leftward in the horizontal direction, which is opposite to that of the mobile screen. Therefore, you need to add a minus sign before #x_gra to ensure that the sensor direction is consistent with the phone direction. When the phone is moving, snow will fall in the direction of the gravity sensor.
<VariableBinders>
<SensorBinder type="gravity">
<Variable name="x_gra" index="0"/>
<Variable name="y_gra" index="1"/>
<Variable name="z_gra" index="2"/>
</SensorBinder>
</VariableBinders>
<DropPhysicalView gravityX="-#x_gra" gravityY="#y_gra" airDensity="50">
<ItemGroup x="0" y="0" width="#screen_width" height="#screen_height">
<Item count="40" src="xuehua.png">
<Velocity isRandom="true" velocityX="0" velocityY="5"/>
<Position isRandom="true"/>
<AngleVelocity isRandom="true" angleVelocity="10"/>
<weight isRandom="true" value="0.5"/>
</Item>
<ItemGroup/>
</DropPhysicalView> Example 2: Implement gravity sensing based on the acceleration sensor. In this example, set type to accelerometer. After obtaining the acceleration in each direction from <SensorBinder>, control the x and y coordinates of the image through #x_acc and #y_acc in <Image>, so that gravity sensing can be implemented. Note that you can control the image moving direction through the sign (minus/plus) before #x_acc and #y_acc and control the moving step through the coefficient multiplied by #x_acc and #y_acc.
<VariableBinders>
<SensorBinder type="accelerometer">
<Variable name="x_acc" index="0"/>
<Variable name="y_acc" index="1"/>
<Variable name="z_acc" index="2"/>
</SensorBinder>
</VariableBinders>
<Image x="610+#x_acc*12" y="390-#y_acc*12" centerX="139" centerY="151" src="ty.png"/>