PhoneGap API帮助文档翻译Accelerometer加速度计是本文要介绍的内容,主要是来了解并学习PhoneGap API文档的翻译,具体PhoneGap API文档的内容来看本文,采集设备在x、y、z方向上的动作。
方法:
- accelerometer.getCurrentAcceleration
- accelerometer.watchAcceleration
- accelerometer.clearWatch
参数:
- accelerometerSuccess
- accelerometerError
- accelerometerOptions
对象(只读):
- Acceleration
- accelerometer.getCurrentAcceleration
返回当前沿x、y和z方向的加速度。
- navigator.accelerometer.getCurrentAcceleration(accelerometerSuccess, accelerometerError);
- navigator.accelerometer.getCurrentAcceleration(accelerometerSuccess, accelerometerError);
PhoneGap API说明:
加速计是检测设备在当前方向上所做相对运动变化(增、减量)的运动传感器。加速计可以检测沿X、Y和Z轴的三维运动。
加速度数据通过accelerometerSuccess回调函数返回。
支持的平台:
Android
BlackBerry WebWorks (OS 5.0或更高版本)
iPhone
简单的范例:
- function onSuccess(acceleration) {
- alert('Acceleration X: ' + acceleration.x + '\n' +
- 'Acceleration Y: ' + acceleration.y + '\n' +
- 'Acceleration Z: ' + acceleration.z + '\n' +
- 'Timestamp: ' + acceleration.timestamp + '\n');
- }
- function onError() {
- alert('onError!');
- }
- navigator.accelerometer.getCurrentAcceleration(onSuccess, onError);
- function onSuccess(acceleration) {
- alert('Acceleration X: ' + acceleration.x + '\n' +
- 'Acceleration Y: ' + acceleration.y + '\n' +
- 'Acceleration Z: ' + acceleration.z + '\n' +
- 'Timestamp: ' + acceleration.timestamp + '\n');
- }
- function onError() {
- alert('onError!');
- }
- navigator.accelerometer.getCurrentAcceleration(onSuccess, onError);
完整的范例:
- </pre><pre name="code" class="html"><!DOCTYPE html>
- <html>
- <head>
- <title>Acceleration Example</title>
- <script type="text/javascript" charset="utf-8" src="phonegap.js"></script>
- <script type="text/javascript" charset="utf-8">
- // 等待加载PhoneGap
- document.addEventListener("deviceready", onDeviceReady, false);
- // PhoneGap加载完毕
- function onDeviceReady() {
- navigator.accelerometer.getCurrentAcceleration(onSuccess, onError);
- }
- // onSuccess: 返回当前加速度数据的快照
- function onSuccess(acceleration) {
- alert('Acceleration X: ' + acceleration.x + '\n' +
- 'Acceleration Y: ' + acceleration.y + '\n' +
- 'Acceleration Z: ' + acceleration.z + '\n' +
- 'Timestamp: ' + acceleration.timestamp + '\n');
- }
- // onError: 返回加速度数据失败
- function onError() {
- alert('onError!');
- }
- </script>
- </head>
- <body>
- <h1>Example</h1>
- <p>getCurrentAcceleration</p>
- </body>
- </html>
- </pre><pre name="code" class="html"><!DOCTYPE html>
- <html>
- <head>
- <title>Acceleration Example</title>
- <script type="text/javascript" charset="utf-8" src="phonegap.js"></script>
- <script type="text/javascript" charset="utf-8">
- // 等待加载PhoneGap
- document.addEventListener("deviceready", onDeviceReady, false);
- // PhoneGap加载完毕
- function onDeviceReady() {
- navigator.accelerometer.getCurrentAcceleration(onSuccess, onError);
- }
- // onSuccess: 返回当前加速度数据的快照
- function onSuccess(acceleration) {
- alert('Acceleration X: ' + acceleration.x + '\n' +
- 'Acceleration Y: ' + acceleration.y + '\n' +
- 'Acceleration Z: ' + acceleration.z + '\n' +
- 'Timestamp: ' + acceleration.timestamp + '\n');
- }
- // onError: 返回加速度数据失败
- function onError() {
- alert('onError!');
- }
- </script>
- </head>
- <body>
- <h1>Example</h1>
- <p>getCurrentAcceleration</p>
- </body>
- </html>
#p#
iPhone的特异情况:
iPhone没有获取在任何给定点当前加速度数据的概念。
你必须通过给定时间间隔查看加速度并获得数据。
因此,getCurrentAcceleration函数会返回从phoneGap watchAccelerometer调用开始后的最近一个返回值
accelerometer.watchAcceleration
在固定的时间间隔获取沿x、y和z轴的加速度。
- var watchID = navigator.accelerometer.watchAcceleration(accelerometerSuccess,
- accelerometerError,
- [accelerometerOptions]);
- var watchID = navigator.accelerometer.watchAcceleration(accelerometerSuccess,
- accelerometerError,
- [accelerometerOptions]);
PhoneGap API说明:
加速计是检测设备在当前方向上所做相对运动变化(增、减量)的动作传感器。加速计可以检测沿X、Y和Z轴的三维运动。
accelerometer.watchAcceleration每隔固定时间就获取一次设备的当前加速度。每次取得加速度后,accelerometerSuccess回调函数会被执行。通过acceleratorOptions对象的frequency参数可以设定以毫秒为单位的时间间隔。
返回的watch id是加速度计监视周期的引用,可以通过accelerometer.clearWatch调用该watch ID以停止对加速度计的监视。
支持的平台:
Android
BlackBerry WebWorks (OS 5.0或更高版本)
iPhone
简单的范例:
- function onSuccess(acceleration) {
- alert('Acceleration X: ' + acceleration.x + '\n' +
- 'Acceleration Y: ' + acceleration.y + '\n' +
- 'Acceleration Z: ' + acceleration.z + '\n' +
- 'Timestamp: ' + acceleration.timestamp + '\n');
- }
- function onError() {
- alert('onError!');
- }
- var options = { frequency: 3000 }; // 每隔3秒更新一次
- var watchID = navigator.accelerometer.watchAcceleration(onSuccess, onError, options);
- function onSuccess(acceleration) {
- alert('Acceleration X: ' + acceleration.x + '\n' +
- 'Acceleration Y: ' + acceleration.y + '\n' +
- 'Acceleration Z: ' + acceleration.z + '\n' +
- 'Timestamp: ' + acceleration.timestamp + '\n');
- }
- function onError() {
- alert('onError!');
- }
- var options = { frequency: 3000 }; // 每隔3秒更新一次
- var watchID = navigator.accelerometer.watchAcceleration(onSuccess, onError, options);
完整的范例:
- <!DOCTYPE html>
- <html>
- <head>
- <title>Acceleration Example</title>
- <script type="text/javascript" charset="utf-8" src="phonegap.js"></script>
- <script type="text/javascript" charset="utf-8">
- // watch id 是当前“watchAcceleration”的引用
- var watchID = null;
- // 等待加载PhoneGap
- document.addEventListener("deviceready", onDeviceReady, false);
- // PhoneGap加载完毕
- function onDeviceReady() {
- startWatch();
- }
- // 开始监视加速度
- function startWatch() {
- // 每隔3秒钟更新一次加速度数据
- var options = { frequency: 3000 };
- watchID = navigator.accelerometer.watchAcceleration(onSuccess, onError, options);
- }
- // 停止监视加速度
- function stopWatch() {
- if (watchID) {
- navigator.accelerometer.clearWatch(watchID);
- watchID = null;
- }
- }
- // onSuccess: 获取当前加速度数据的快照
- function onSuccess(acceleration) {
- var element = document.getElementById('accelerometer');
- element.innerHTML = 'Acceleration X: ' + acceleration.x + '<br />' +
- 'Acceleration Y: ' + acceleration.y + '<br />' +
- 'Acceleration Z: ' + acceleration.z + '<br />' +
- 'Timestamp: ' + acceleration.timestamp + '<br />';
- }
- // onError: 获取加速度失败
- function onError() {
- alert('onError!');
- }
- </script>
- </head>
- <body>
- <div id="accelerometer">Waiting for accelerometer...</div>
- </body>
- </html>
- <!DOCTYPE html>
- <html>
- <head>
- <title>Acceleration Example</title>
- <script type="text/javascript" charset="utf-8" src="phonegap.js"></script>
- <script type="text/javascript" charset="utf-8">
- // watch id 是当前“watchAcceleration”的引用
- var watchID = null;
- // 等待加载PhoneGap
- document.addEventListener("deviceready", onDeviceReady, false);
- // PhoneGap加载完毕
- function onDeviceReady() {
- startWatch();
- }
- // 开始监视加速度
- function startWatch() {
- // 每隔3秒钟更新一次加速度数据
- var options = { frequency: 3000 };
- watchID = navigator.accelerometer.watchAcceleration(onSuccess, onError, options);
- }
- // 停止监视加速度
- function stopWatch() {
- if (watchID) {
- navigator.accelerometer.clearWatch(watchID);
- watchID = null;
- }
- }
- // onSuccess: 获取当前加速度数据的快照
- function onSuccess(acceleration) {
- var element = document.getElementById('accelerometer');
- element.innerHTML = 'Acceleration X: ' + acceleration.x + '<br />' +
- 'Acceleration Y: ' + acceleration.y + '<br />' +
- 'Acceleration Z: ' + acceleration.z + '<br />' +
- 'Timestamp: ' + acceleration.timestamp + '<br />';
- }
- // onError: 获取加速度失败
- function onError() {
- alert('onError!');
- }
- </script>
- </head>
- <body>
- <div id="accelerometer">Waiting for accelerometer...</div>
- </body>
- </html>
#p#
iPhone的特异情况:
PhoneGap API在请求的时间间隔,PhoneGap将调用success回调指向的函数,并传递加速度计数据。
不过,PhoneGap将对设备的请求间隔时间限制为最小40ms,最大1000ms。
例如,如果你设定每隔3秒(3000毫秒)请求一次,PhoneGap仍然每隔1秒请求一次设备,但是每隔3秒才调用一次success回调函数。
accelerometer.clearWatch
停止watch ID参数指向的加速度监视。
- navigator.accelerometer.clearWatch(watchID);
- navigator.accelerometer.clearWatch(watchID);
watchID:由accelerometer.watchAcceleration返回的引用标识ID。
支持的平台:
Android
BlackBerry WebWorks (OS 5.0或更高版本)
iPhone
简单的范例:
- var watchID = navigator.accelerometer.watchAcceleration(onSuccess, onError, options);
- // ... 后续处理 ...
- navigator.accelerometer.clearWatch(watchID);
- var watchID = navigator.accelerometer.watchAcceleration(onSuccess, onError, options);
- // ... 后续处理 ...
- navigator.accelerometer.clearWatch(watchID);
完整的范例:
- <!DOCTYPE html>
- <html>
- <head>
- <title>Acceleration Example</title>
- <script type="text/javascript" charset="utf-8" src="phonegap.js"></script>
- <script type="text/javascript" charset="utf-8">
- // watch id 是当前“watchAcceleration”的引用
- var watchID = null;
- // 等待加载PhoneGap
- document.addEventListener("deviceready", onDeviceReady, false);
- // PhoneGap加载完毕
- function onDeviceReady() {
- startWatch();
- }
- // 开始监视加速度
- function startWatch() {
- // 每隔3秒钟更新一次加速度数据
- var options = { frequency: 3000 };
- watchID = navigator.accelerometer.watchAcceleration(onSuccess, onError, options);
- }
- // 停止监视加速度
- function stopWatch() {
- if (watchID) {
- navigator.accelerometer.clearWatch(watchID);
- watchID = null;
- }
- }
- // onSuccess: 获取当前加速度数据的快照
- function onSuccess(acceleration) {
- var element = document.getElementById('accelerometer');
- element.innerHTML = 'Acceleration X: ' + acceleration.x + '<br />' +
- 'Acceleration Y: ' + acceleration.y + '<br />' +
- 'Acceleration Z: ' + acceleration.z + '<br />' +
- 'Timestamp: ' + acceleration.timestamp + '<br />';
- }
- // onError: 获取加速度失败
- function onError() {
- alert('onError!');
- }
- </script>
- </head>
- <body>
- <div id="accelerometer">Waiting for accelerometer...</div>
- <button onclick="stopWatch();">Stop Watching</button>
- </body>
- </html>
- <!DOCTYPE html>
- <html>
- <head>
- <title>Acceleration Example</title>
- <script type="text/javascript" charset="utf-8" src="phonegap.js"></script>
- <script type="text/javascript" charset="utf-8">
- // watch id 是当前“watchAcceleration”的引用
- var watchID = null;
- // 等待加载PhoneGap
- document.addEventListener("deviceready", onDeviceReady, false);
- // PhoneGap加载完毕
- function onDeviceReady() {
- startWatch();
- }
- // 开始监视加速度
- function startWatch() {
- // 每隔3秒钟更新一次加速度数据
- var options = { frequency: 3000 };
- watchID = navigator.accelerometer.watchAcceleration(onSuccess, onError, options);
- }
- // 停止监视加速度
- function stopWatch() {
- if (watchID) {
- navigator.accelerometer.clearWatch(watchID);
- watchID = null;
- }
- }
- // onSuccess: 获取当前加速度数据的快照
- function onSuccess(acceleration) {
- var element = document.getElementById('accelerometer');
- element.innerHTML = 'Acceleration X: ' + acceleration.x + '<br />' +
- 'Acceleration Y: ' + acceleration.y + '<br />' +
- 'Acceleration Z: ' + acceleration.z + '<br />' +
- 'Timestamp: ' + acceleration.timestamp + '<br />';
- }
- // onError: 获取加速度失败
- function onError() {
- alert('onError!');
- }
- </script>
- </head>
- <body>
- <div id="accelerometer">Waiting for accelerometer...</div>
- <button onclick="stopWatch();">Stop Watching</button>
- </body>
- </html>
#p#
Acceleration
包含特定时间点采集到的加速计数据。
属性:
x:在X轴的运动量,[0, 1]范围(数字类型)
y:在Y轴的运动量,[0, 1]范围(数字类型)
z:在Z轴的运动量,[0, 1]范围(数字类型)
timestamp:以毫秒为单位的创建时间戳。(DOMTimeStamp类型)
PhoneGap API说明:
这个对象是由phoneGap创建和填充,并由Acce这个对象是由PhoneGap创建和填充,并由Accelerometer的方法返回。
支持的平台:
Android
BlackBerry WebWorks (OS 5.0或更高版本)
iPhone
简单的范例:
- function onSuccess(acceleration) {
- alert('Acceleration X: ' + acceleration.x + '\n' +
- 'Acceleration Y: ' + acceleration.y + '\n' +
- 'Acceleration Z: ' + acceleration.z + '\n' +
- 'Timestamp: ' + acceleration.timestamp + '\n');
- }
- function onError() {
- alert('onError!');
- }
- navigator.accelerometer.getCurrentAcceleration(onSuccess,onError);
- function onSuccess(acceleration) {
- alert('Acceleration X: ' + acceleration.x + '\n' +
- 'Acceleration Y: ' + acceleration.y + '\n' +
- 'Acceleration Z: ' + acceleration.z + '\n' +
- 'Timestamp: ' + acceleration.timestamp + '\n');
- }
- function onError() {
- alert('onError!');
- }
- navigator.accelerometer.getCurrentAcceleration(onSuccess,onError);
完整的范例:
- <!DOCTYPE html>
- <html>
- <head>
- <title>Acceleration Example</title>
- <script type="text/javascript" charset="utf-8" src="phonegap.js"></script>
- <script type="text/javascript" charset="utf-8">
- // 等待加载PhoneGap
- document.addEventListener("deviceready", onDeviceReady, false);
- // PhoneGap加载完毕
- function onDeviceReady() {
- navigator.accelerometer.getCurrentAcceleration(onSuccess, onError);
- }
- // onSuccess: 获得加速度数据的快照
- function onSuccess(acceleration) {
- alert('Acceleration X: ' + acceleration.x + '\n' +
- 'Acceleration Y: ' + acceleration.y + '\n' +
- 'Acceleration Z: ' + acceleration.z + '\n' +
- 'Timestamp: ' + acceleration.timestamp + '\n');
- }
- // onError: 获取加速度失败
- function onError() {
- alert('onError!');
- }
- </script>
- </head>
- <body>
- <h1>Example</h1>
- <p>getCurrentAcceleration</p>
- </body>
- </html>
- <!DOCTYPE html>
- <html>
- <head>
- <title>Acceleration Example</title>
- <script type="text/javascript" charset="utf-8" src="phonegap.js"></script>
- <script type="text/javascript" charset="utf-8">
- // 等待加载PhoneGap
- document.addEventListener("deviceready", onDeviceReady, false);
- // PhoneGap加载完毕
- function onDeviceReady() {
- navigator.accelerometer.getCurrentAcceleration(onSuccess, onError);
- }
- // onSuccess: 获得加速度数据的快照
- function onSuccess(acceleration) {
- alert('Acceleration X: ' + acceleration.x + '\n' +
- 'Acceleration Y: ' + acceleration.y + '\n' +
- 'Acceleration Z: ' + acceleration.z + '\n' +
- 'Timestamp: ' + acceleration.timestamp + '\n');
- }
- // onError: 获取加速度失败
- function onError() {
- alert('onError!');
- }
- </script>
- </head>
- <body>
- <h1>Example</h1>
- <p>getCurrentAcceleration</p>
- </body>
- </html>
- accelerometerSuccess
提供加速度信息的onSuccess回调函数。
- function(acceleration) {
- // 进一步处理
- }
- function(acceleration) {
- // 进一步处理
- }
参数:
acceleration: 在某一时刻的加速度(Acacceleration:在某一时刻的加速度。(Acceleration对象类型)
- function onSuccess(acceleration) {
- alert('Acceleration X: ' + acceleration.x + '\n' +
- 'Acceleration Y: ' + acceleration.y + '\n' +
- 'Acceleration Z: ' + acceleration.z + '\n' +
- 'Timestamp: ' + acceleration.timestamp + '\n');
- }
- function onSuccess(acceleration) {
- alert('Acceleration X: ' + acceleration.x + '\n' +
- 'Acceleration Y: ' + acceleration.y + '\n' +
- 'Acceleration Z: ' + acceleration.z + '\n' +
- 'Timestamp: ' + acceleration.timestamp + '\n');
- }
- accelerometerError
加速度方法的onError回调函数。
- function() {
- // 错误处理
- }
- function() {
- // 错误处理
- }
- accelerometerOptions
定制检索加速度计的可选参数。
选项:
frequency:多少毫秒获取一次Acceleration。(数字类型)(默认值:10000)。
小结:解析PhoneGap API帮助文档翻译Accelerometer加速度计的内容介绍完了,希望通过PhoneGap API内容的学习能对你有所帮助!