问题描述:
我的朋友和我正在教自己开发android。我们调试了其他类,比如游戏物理和手动更新的值,它们将大理石横跨我们的板。当我们改回来读取加速度计数据时,大理石退出了移动。因此,我们将包括我们认为问题正在发生的类。基本上,我们在android开发中的视图类。
我们正在自学android开发,我们无法理解为什么来自加速计的数据,我们称之为mSensorX和mSensorY的数据没有被更新。我们在下面包含的是我们的视图类。请注意,我们正在为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项目中获得了很多我们的想法和方法。但是,那些熟悉该代码的人可以看到我们竭尽全力尝试简化命名约定,方法和调用。
基本上,我们非常感谢您的帮助。这就像传感器是“那里”,但实际上它永远不会被使用。例如,当我们向其添加调试标记时,非常有用的是视图从不调用方法onSensorChanged 。因此,另一个案例是我们的加速度计真的不起作用。
答案 0 :(得分:3)
您需要在传感器管理器中注册您的实例,如下所示:
mSensorManager.registerListener(this, mAccelerometer,
SensorManager.SENSOR_DELAY_NORMAL);