gpt4 book ai didi

java - 为什么我无法从加速度计传感器获取数据?

转载 作者:行者123 更新时间:2023-12-02 00:07:27 25 4
gpt4 key购买 nike

问题描述:

我和我的 friend 正在自学 Android 开发。我们调试了其他类,例如游戏物理和手动更新的值,这使得弹珠沿着我们的棋盘对角线移动。当我们将其改回读取加速度计数据时,大理石停止移动。因此,我们将包括我们认为发生问题的类。基本上就是我们在android开发中的 View 类。

我们正在自学 Android 开发,我们无法理解为什么来自加速度计(我们称之为 mSensorX 和 mSensorY)的数据没有被更新。下面包含的是我们的 View 类。请注意,我们正在针对 android 3.1 进行开发。然而,我们在 Eclipse 中使用了 Android 的所有最新功能。

package com.example.marblez;


import com.example.marblez.R;
import android.app.Activity;
import android.content.Context;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.util.Log;
import android.view.Display;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.Surface;
import android.view.View;
import android.view.WindowManager;

public class MarblezView extends View implements SensorEventListener{


//Sensor Stuff
private SensorManager mSensorManager;
private Display mDisplay;


//Accelerometer sensor stuff
private Sensor mAccelerometer;
private float mSensorX;
private float mSensorY;

//Variables related to time
private long mCpuTimeStamp;
private long mSensorTimeStamp;
private WindowManager mWindowManager;



//Accelerometer buffer, such that slight movement will roll the marble
private float sensor_buffer = 0;

//Create the canvas
private Canvas mCanvas;
private MarblezBackground background;

//Create the marble
private Marblez ball;

//Constructor for Marblez View
@SuppressWarnings("deprecation")
public MarblezView(Context context, Activity activity){
super(context);

//Setup sensor stuff
mSensorManager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
mWindowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
mDisplay = mWindowManager.getDefaultDisplay();

//Set up Maze
background = new MarblezBackground(activity);

//Create our rolling little friend :-)
ball = new Marblez(activity);

}

@Override
public void onSensorChanged(SensorEvent event) {
if (event.sensor.getType() != Sensor.TYPE_ACCELEROMETER)
return;
/*
* record the accelerometer data, the event's timestamp as well as
* the current time. The latter is needed so we can calculate the
* "present" time during rendering. In this application, we need to
* take into account how the screen is rotated with respect to the
* sensors (which always return data in a coordinate space aligned
* to with the screen in its native orientation).
*/

switch (mDisplay.getRotation()) {
case Surface.ROTATION_0:
mSensorX = event.values[0];
mSensorY = event.values[1];
break;
case Surface.ROTATION_90:
mSensorX = -event.values[1];
mSensorY = event.values[0];
break;
case Surface.ROTATION_180:
mSensorX = -event.values[0];
mSensorY = -event.values[1];
break;
case Surface.ROTATION_270:
mSensorX = event.values[1];
mSensorY = -event.values[0];
break;
}

mSensorTimeStamp = event.timestamp;
mCpuTimeStamp = System.nanoTime();
}


//Automatic call
@Override
public void onDraw(Canvas canvas){
mCanvas = canvas;

//Draw the maze
background.drawMaze(canvas);

//Get the x and y sensor data and other goodies
final long now = mSensorTimeStamp + (System.nanoTime() - mCpuTimeStamp);

//Draw the marble
ball.drawMarble(canvas, mSensorX, mSensorY, now);

//Invalidate so it draws again
invalidate();

}

@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}

}

我们从一个名为 Accelerometer Play 的开源 Google 项目中获得了很多想法和方法。然而,熟悉该代码的人可以看到我们竭尽全力尝试简化命名约定、方法和调用。

基本上,我们非常感谢您的帮助。就好像传感器“在那里”,但出于实际目的它从未被使用过。例如,很明显,当我们向 View 添加调试标记时, View 从未调用方法 onSensorChanged。因此,另一种情况是我们的加速度计确实不起作用。

最佳答案

您需要在传感器管理器中注册您的实例,如下所示:

mSensorManager.registerListener(this, mAccelerometer,
SensorManager.SENSOR_DELAY_NORMAL);

关于java - 为什么我无法从加速度计传感器获取数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13556668/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com