accelerometer模塊主要用於管理設備加速度傳感器。
用於獲取設備加速度信息,包括x(屏幕水平方向)、y(垂直屏幕水平方向)、z(垂直屏幕平面方向)三個方向的加速度信息。
該模塊有三個方法
1、getCurrentAcceleration: 獲取當前設備的加速度信息
2、watchAcceleration: 監聽設備加速度變化信息
3、clearWatch: 關閉監聽設備加速度信息
getCurrentAcceleration方法可以獲取當前設備的加速度信息
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0, user-scalable=no"/>
<title>心得</title>
<script type="text/javascript">
//監聽plusready事件
document.addEventListener("plusready",xhrs);//系統准備好后執行 自定義函數xhrs
function xhrs(){
plus.accelerometer.getCurrentAcceleration(
function(xhrs_a){
alert( "加速信息\nx:" + xhrs_a.xAxis + "\ny:" + xhrs_a.yAxis + "\nz:" + xhrs_a.zAxis );
},
function(){
alert("當前設備無法獲取加速信息');
}
);
}
//getCurrentAcceleration(成功獲取加速信息的回調函數有返回值,獲取加速信息失敗時調用的函數)
</script>
</head>
<body>1
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0, user-scalable=no"/>
<title>心得</title>
<script type="text/javascript">
//監聽plusready事件
var
xhrs_wid='';//外部聲明一個變量用於給 setTimeout使用,不然xhrs_wid就是addEventListener函數內的局部變量了
document.addEventListener("plusready",xhrs);//系統准備好后執行 自定義函數xhrs
function xhrs(){
xhrs_wid = plus.accelerometer.watchAcceleration(
function(xhrs_a){
document.getElementById("aa").innerHTML = Math.random();//
},function(){document.getElementById("aa").innerHTML = '無法獲取加速信息';},
{frequency:2000}//2秒一次的執行該函數,默認為500
);
}//watchAcceleration方法調用完后會有一個返回值
//watchAcceleration(成功獲取加速信息的回調函數有參數【必須】,獲取加速信息失敗時調用的函數【可選】, {frequency:執行評率毫秒計時} )
//watchAcceleration類似於setInterval
setTimeout(function(){plus.accelerometer.clearWatch(xhrs_wid)},5000);//5秒后關閉加速計算
</script>
</head>
<body>
<div id="aa">1321331231</div>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0, user-scalable=no"/>
<title>心得</title>
<script type="text/javascript">
document.addEventListener("plusready",xhrs);//監聽plusready事件
function xhrs(){
plus.accelerometer.watchAcceleration(
function(xhrs_a){
document.getElementById("aa").innerHTML = "x軸方向的加速度="+xhrs_a.xAxis;
},
function(){document.getElementById("aa").innerHTML = '無法獲取加速信息';},
{frequency:2000}
);
}
</script>
</head>
<body>
<div id="aa"><!--在此處顯示加速信息--></div>
</body>
</html>