android 陀螺仪 加速度定位,Android陀螺仪版加速度计

我正在构建一个Android游戏,我想弄清楚用户是否向左或向右倾斜设备(类似于当你将人从一侧移到另一侧时Temple Run的工作方式).

我已经阅读了很多教程和示例,并且我制作了示例应用程序,但是从陀螺仪和加速度计中获取的数据量都是压倒性的.我是否需要两套硬件来确定用户是否倾斜设备以及在哪个方向?

我目前的应用是检测每一个轻微的动作,这显然是不正确的.

public class Main extends Activity {

private SensorManager mSensorManager;

private float mAccel; // acceleration apart from gravity

private float mAccelCurrent; // current acceleration including gravity

private float mAccelLast; // last acceleration including gravity

private RelativeLayout background;

private Boolean isleft = true;

/** Called when the activity is first created. */

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

this.background = (RelativeLayout) findViewById(R.id.RelativeLayout1);

mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);

mSensorManager.registerListener(mSensorListener, mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_NORMAL);

mAccel = 0.00f;

mAccelCurrent = SensorManager.GRAVITY_EARTH;

mAccelLast = SensorManager.GRAVITY_EARTH;

/* float x1, x2, y1, y2;

String direction;

switch(event.getAction()) {

case(MotionEvent.ACTION_DOWN):

x1 = event.getX();

y1 = event.getY();

break;

case(MotionEvent.ACTION_UP) {

x2 = event.getX();

y2 = event.getY();

float dx = x2-x1;

float dy = y2-y1;

// Use dx and dy to determine the direction

if(Math.abs(dx) > Math.abs(dy)) {

if(dx>0) directiion = "right";

else direction = "left";

} else {

if(dy>0) direction = "down";

else direction = "up";

}

}

}*/

}

private final SensorEventListener mSensorListener = new SensorEventListener() {

public void onSensorChanged(SensorEvent se) {

float x = se.values[0];

float y = se.values[1];

float z = se.values[2];

if((mAccelLast

background.setBackgroundResource(R.drawable.bg_right);

isleft = false;

}

if((mAccelLast>mAccelCurrent)&&(isleft == false)){

background.setBackgroundResource(R.drawable.bg_left);

isleft = true;

}

mAccelLast = mAccelCurrent;

mAccelCurrent = (float) Math.sqrt((double) (x*x + y*y + z*z));

float delta = mAccelCurrent - mAccelLast;

Log.d("FB", "delta : "+delta);

mAccel = mAccel * 0.9f + delta; // perform low-cut filter

// Log.d("FB", "mAccel : "+mAccel);

}

我最好只使用加速度计,只使用陀螺仪还是我需要两者?

解决方法:

根据我非常有限的经验,陀螺仪不断测量x,y,z旋转并不断更新.用于在游戏中驾驶汽车/飞机/角色.加速度计有点像wii-mote,用于摆动或拾取摇动手势.

标签:android,accelerometer,gyroscope,motion-detection

来源: https://codeday.me/bug/20190729/1571599.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值