03模块-accelerometer

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>Accelerometer Example</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>Accelerometer Example</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>Accelerometer Example</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>

猜你喜欢

转载自www.cnblogs.com/xhrs/p/9317032.html