问题描述:
我和我的朋友正在自学安卓开发。我们调试了其他类,比如游戏物理和手动更新的值,这使得大理石在我们的板上对角线。当我们将其改回读取加速度计数据时,弹珠停止移动。因此,我们将包括我们认为发生问题的类。基本上,我们在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 的开源谷歌项目中获得了很多想法和方法。但是,熟悉该代码的人可以看到,我们竭尽全力尝试简化命名约定、方法和调用。
基本上,我们将感谢您的帮助。就像传感器“在那里”一样,但出于实际目的,它从未使用过。例如,当我们向它添加调试标记时,视图永远不会调用onSensorChanged 方法,这很能说明问题。因此,我们的加速度计确实无法正常工作的另一种情况。