立即注册 登录
PhoneGap中文网 返回首页

niexiangli的个人空间 http://www.phonegap100.com/?6884 [收藏] [复制] [RSS]

日志

Accelerometer(加速度计)

已有 1270 次阅读2014-4-14 16:06 |个人分类:分享日志| 加速度

捕捉的xy装置的动作和z方向。

方法

· accelerometer.getCurrentAcceleration

· accelerometer.watchAcceleration

· accelerometer.clearWatch

参数

· accelerometerSuccess

· accelerometerError

· accelerometerOptions

对象(只读)

· Acceleration

accelerometer.getCurrentAcceleration

相处的XY轴电流加速,和Z轴。

navigator.accelerometer.getCurrentAcceleration(accelerometerSuccessaccelerometerError);

Description说明

加速度计是一个运动传感器,它检测到的运动变化(增量)相对于当前设备的方向。 加速度计可以检测沿的xy三维运动,和Z轴。

加速返回使用 accelerometerSuccess 回调函数。

支持的平台

· Android

· BlackBerry WebWorks (OS 5.0 and higher)

· 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);

完整的例子:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
                      "http://www.w3.org/TR/html4/strict.dtd">
<html>
  <head>
    <title>Acceleration Example</title>

    <script type="text/javascript" charset="utf-8" src="phonegap.js"></script>
    <script type="text/javascript" charset="utf-8">

    // Wait for PhoneGap to load
    //
    function onLoad() {
        document.addEventListener("deviceready", onDeviceReady, false);
    }

    // PhoneGap is ready
    //
    function onDeviceReady() {
        navigator.accelerometer.getCurrentAcceleration(onSuccess, onError);
    }

    // onSuccess: Get a snapshot of the current acceleration
    //
    function onSuccess(acceleration) {
        alert('Acceleration X: ' + acceleration.x + '\n' +
              'Acceleration Y: ' + acceleration.y + '\n' +
              'Acceleration Z: ' + acceleration.z + '\n' +
              'Timestamp: '      + acceleration.timestamp + '\n');
    }

    // onError: Failed to get the acceleration
    //
    function onError() {
        alert('onError!');
    }

    </script>
  </head>
  <body onload="onLoad()">
    <h1>Example</h1>
    <p>getCurrentAcceleration


  </body>
</html>

iPhone 缺陷:

· iPhone不具备的是在任何特定点当前加速度的概念。

· 你必须看的加速和在给定的时间间隔捕获数据。

· 因此, getCurrentAcceleration 功能会给你的最后一个值phoneGap报告从 watchAccelerometer 电话。

accelerometer.watchAcceleration

在定期的时间间隔,相处的XY轴加速度,和Z轴。

var watchID = navigator.accelerometer.watchAcceleration(accelerometerSuccess,
                                                       accelerometerError,
                                                       [accelerometerOptions]);

说明

加速度计是一个运动传感器,它检测到的运动变化(增量)相对于当前的位置。 加速度计可以检测沿的xy三维运动,和Z轴。

 accelerometer.watchAcceleration 获得加速度定期间隔设备的电流。 每次 加速 的检索, accelerometerSuccess 回调函数被执行。 通过指定的时间间隔以毫秒为单位 的频率 参数在 acceleratorOptions 对象。

返回的手表ID引用引用加速度计手表的时间间隔。 这款手表ID可用于 accelerometer.clearWatch 停止观看加速度计。

支持的平台

· Android

· BlackBerry WebWorks (OS 5.0 and higher)

· 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 };  // Update every 3 seconds

var watchID = navigator.accelerometer.watchAcceleration(onSuccess, onError, options);

完整的例子

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
                      "http://www.w3.org/TR/html4/strict.dtd">
<html>
  <head>
    <title>Acceleration Example</title>

    <script type="text/javascript" charset="utf-8" src="phonegap.js"></script>
    <script type="text/javascript" charset="utf-8">

    // The watch id references the current `watchAcceleration`
    var watchID = null;

    // Wait for PhoneGap to load
    //
    function onLoad() {
        document.addEventListener("deviceready", onDeviceReady, false);
    }

    // PhoneGap is ready
    //
    function onDeviceReady() {
        startWatch();
    }

    // Start watching the acceleration
    //
    function startWatch() {

        // Update acceleration every 3 seconds
        var options = { frequency: 3000 };

        watchID = navigator.accelerometer.watchAcceleration(onSuccess, onError, options);
    }

    // Stop watching the acceleration
    //
    function stopWatch() {
        if (watchID) {
            navigator.accelerometer.clearWatch(watchID);
            watchID = null;
        }
    }

    // onSuccess: Get a snapshot of the current acceleration
    //
    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: Failed to get the acceleration
    //
    function onError() {
        alert('onError!');
    }

    </script>
  </head>
  <body onload="onLoad()">
    <div id="accelerometer">Waiting for accelerometer...</div>
  </body>
</html>

iPhone 缺陷:

· 在要求的时间间隔,PhoneGap将调用成功的回调函数并传递加速度计的结果。

· 然而,在对设备的要求PhoneGap限制到最小间隔每40毫秒和每1000毫秒最大。

· 例如,如果您要求的3秒(3000ms)间隔,PhoneGap将要求从设备的时间间隔为1秒,但在调用请求3秒间隔的成功回调。

accelerometer.clearWatch

不看 加速 引用手表ID参数。

navigator.accelerometer.clearWatch(watchID);

· watchID返回的ID  accelerometer.watchAcceleration.

 

支持的平台

· Android

· BlackBerry WebWorks (OS 5.0 and higher)

· iPhone

简单的例子:

var watchID = navigator.accelerometer.watchAcceleration(onSuccess, onError, options);

// ... later on ...

navigator.accelerometer.clearWatch(watchID);

完整的例子:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
                      "http://www.w3.org/TR/html4/strict.dtd">
<html>
  <head>
    <title>Acceleration Example</title>

    <script type="text/javascript" charset="utf-8" src="phonegap.js"></script>
    <script type="text/javascript" charset="utf-8">

    // The watch id references the current `watchAcceleration`
    var watchID = null;

    // Wait for PhoneGap to load
    //
    function onLoad() {
        document.addEventListener("deviceready", onDeviceReady, false);
    }

    // PhoneGap is ready
    //
    function onDeviceReady() {
        startWatch();
    }

    // Start watching the acceleration
    //
    function startWatch() {

        // Update acceleration every 3 seconds
        var options = { frequency: 3000 };

        watchID = navigator.accelerometer.watchAcceleration(onSuccess, onError, options);
    }

    // Stop watching the acceleration
    //
    function stopWatch() {
        if (watchID) {
            navigator.accelerometer.clearWatch(watchID);
            watchID = null;
        }
    }

    // onSuccess: Get a snapshot of the current acceleration
    //
    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: Failed to get the acceleration
    //
    function onError() {
        alert('onError!');
    }

    </script>
  </head>
  <body onload="onLoad()">
    <div id="accelerometer">Waiting for accelerometer...</div>
    <button onclick="stopWatch();">Stop Watching</button>
  </body>
</html>

Acceleration

包含 加速度计 数据捕获在特定的时间点。

属性

· 谢: 对运动量的X轴。 区间[01]  

· Ÿ 轴金 ​​额的运动在Y 区间[01]  

· 张: 轴金 ​​额的运动在z 区间[01]  

· 时间戳: 时间戳毫秒创作研究。  DOMTimeStamp 

说明

这个对象是通过创建和PhoneGap人口,以及返回的 加速度 的方法。

支持的平台

· Android

· BlackBerry WebWorks (OS 5.0 and higher)

· 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);

完整的例子:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
                      "http://www.w3.org/TR/html4/strict.dtd">
<html>
  <head>
    <title>Acceleration Example</title>

    <script type="text/javascript" charset="utf-8" src="phonegap.js"></script>
    <script type="text/javascript" charset="utf-8">

    // Wait for PhoneGap to load
    //
    function onLoad() {
        document.addEventListener("deviceready", onDeviceReady, false);
    }

    // PhoneGap is ready
    //
    function onDeviceReady() {
        navigator.accelerometer.getCurrentAcceleration(onSuccess, onError);
    }

    // onSuccess: Get a snapshot of the current acceleration
    //
    function onSuccess() {
        alert('Acceleration X: ' + acceleration.x + '\n' +
              'Acceleration Y: ' + acceleration.y + '\n' +
              'Acceleration Z: ' + acceleration.z + '\n' +
              'Timestamp: '      + acceleration.timestamp + '\n');
    }

    // onError: Failed to get the acceleration
    //
    function onError() {
        alert('onError!');
    }

    </script>
  </head>
  <body onload="onLoad()">
    <h1>Example</h1>
    <p>getCurrentAcceleration


  </body>
</html>

accelerometerSuccess

onSuccess 回调函数,提供了 Acceleration 的信息。

 

function(acceleration) {
    // 做什么
}

参数

· acceleration: 加速时间在一个单一的时刻。 (Acceleration)

Example

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() {
    // Handle the error
}

accelerometerOptions

一个可选的参数来定制加速检索。

选项

· 频率: 多久检索 Acceleration  以毫秒为单位。 (数目) (默认:10000


头晕

不解

扯淡

搞笑

欠扁

超赞

支持

难过

评论 (0 个评论)

facelist

您需要登录后才可以评论 登录 | 立即注册

关闭

站长推荐 上一条 /1 下一条

ionic4视频教程

Archiver|手机版|小黑屋| PhoneGap中文网 ( 京ICP备13027796号-1 )  

GMT+8, 2024-5-5 21:22 , Processed in 0.037366 second(s), 22 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

返回顶部