Android 加速度计不改变数值
所以我有下面的代码,我无法工作。我试图让一个类(AccelerometerReader)读取手机加速计的值,然后我在另一个类(MyGame)中调用这些值,我用它在屏幕上打印这些值。代码中的一切看起来都很好,我没有得到任何值,除了我打印的值都是0.0,并且没有改变。我知道我的手机也有一个加速计。任何帮助都将不胜感激 谢谢,欧文 加速计读卡器等级Android 加速度计不改变数值,android,accelerometer,Android,Accelerometer,所以我有下面的代码,我无法工作。我试图让一个类(AccelerometerReader)读取手机加速计的值,然后我在另一个类(MyGame)中调用这些值,我用它在屏幕上打印这些值。代码中的一切看起来都很好,我没有得到任何值,除了我打印的值都是0.0,并且没有改变。我知道我的手机也有一个加速计。任何帮助都将不胜感激 谢谢,欧文 加速计读卡器等级 import android.app.Activity; import android.hardware.Sensor; import android.h
import android.app.Activity;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
public class AccelerometerReader extends Activity implements SensorEventListener{
private SensorManager sensorManager;
float ax,ay,az; // these are the acceleration in x, y and z axes
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
sensorManager=(SensorManager) getSystemService(SENSOR_SERVICE);
sensorManager.registerListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_NORMAL);
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
@Override
public void onSensorChanged(SensorEvent event) {
if (event.sensor.getType() != Sensor.TYPE_ACCELEROMETER)
return;
if (event.sensor.getType()==Sensor.TYPE_ACCELEROMETER){
ax=event.values[0];
ay=event.values[1];
az=event.values[2];
}
}
public float getValueX() {
return ax;
}
public float getValueY() {
return ay;
}
public float getValueZ() {
return az;
}
}
@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
leftWall = new Rect(-100,0,0,canvas.getHeight());
rightWall = new Rect(canvas.getWidth(), 0, (canvas.getWidth() + 100), canvas.getHeight());
floor = new Rect(0,canvas.getHeight(),canvas.getWidth(),(canvas.getHeight() + 100));
ceiling = new Rect(0,-100,canvas.getWidth(),0);
AccelerometerReader acc = new AccelerometerReader();
float ax = acc.getValueX();
float ay = acc.getValueY();
float az = acc.getValueZ();
canvas.drawColor(Color.rgb(red,green,blue)); //0 or 51?, 51, 102
Rect player = new Rect(xPos,yPos,xPos+100,yPos+100);
Paint myPaint = new Paint();
myPaint.setColor(Color.rgb(0, 102, 255));
canvas.drawRect(player, myPaint);
myPaint.setColor(Color.rgb(0, 0, 0));
canvas.drawRect(xPos + 5, yPos + 5, xPos + 95, yPos +95, myPaint);
myPaint.setColor(Color.WHITE);
myPaint.setTextSize(20);
canvas.drawText("" + Math.round(ySpeed), 75, 50, myPaint);
canvas.drawText(ax +", "+ ay +", "+ az, 300, 50, myPaint);
if(gameStarted == false) {
myPaint.setTextSize(30);
myPaint.setTextAlign(Align.CENTER);
canvas.drawText("Touch screen to begin!", canvas.getWidth()/2, canvas.getHeight()/2, myPaint);
}
int r = Math.round(rand.nextInt(canvas.getHeight() - 150));
if(needsRand == true) {
blockY = r;
myPaint.setColor(Color.rgb(rand.nextInt(255), rand.nextInt(255), rand.nextInt(255)));
}
//if(isAlive == true) {
Rect block = new Rect(blockX,blockY,(blockX+100),(blockY+100)); // left, top, right, bottom
myPaint.setColor(Color.WHITE);
canvas.drawRect(block, myPaint);
blockX--;
//}
if(player.intersect(leftWall)) {
xSpeed = Math.abs(xSpeed);
} else if(player.intersect(rightWall)) {
xSpeed = -(xSpeed);
} else if(player.intersect(floor)) {
yPos = canvas.getHeight() - 150;
ySpeed = -(ySpeed)*.75;
} else if(player.intersect(ceiling)) {
ySpeed = Math.abs(ySpeed);
} else if(player.intersect(block)) {
ySpeed = 0;
}
if(blockX <= -100) {
blockX = canvas.getWidth() + 100;
needsRand = true;
} else {
needsRand = false;
}
canvas.drawText("" + r, 300, 100, myPaint);
physics();
invalidate();
}
在MyGame类中
import android.app.Activity;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
public class AccelerometerReader extends Activity implements SensorEventListener{
private SensorManager sensorManager;
float ax,ay,az; // these are the acceleration in x, y and z axes
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
sensorManager=(SensorManager) getSystemService(SENSOR_SERVICE);
sensorManager.registerListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_NORMAL);
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
@Override
public void onSensorChanged(SensorEvent event) {
if (event.sensor.getType() != Sensor.TYPE_ACCELEROMETER)
return;
if (event.sensor.getType()==Sensor.TYPE_ACCELEROMETER){
ax=event.values[0];
ay=event.values[1];
az=event.values[2];
}
}
public float getValueX() {
return ax;
}
public float getValueY() {
return ay;
}
public float getValueZ() {
return az;
}
}
@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
leftWall = new Rect(-100,0,0,canvas.getHeight());
rightWall = new Rect(canvas.getWidth(), 0, (canvas.getWidth() + 100), canvas.getHeight());
floor = new Rect(0,canvas.getHeight(),canvas.getWidth(),(canvas.getHeight() + 100));
ceiling = new Rect(0,-100,canvas.getWidth(),0);
AccelerometerReader acc = new AccelerometerReader();
float ax = acc.getValueX();
float ay = acc.getValueY();
float az = acc.getValueZ();
canvas.drawColor(Color.rgb(red,green,blue)); //0 or 51?, 51, 102
Rect player = new Rect(xPos,yPos,xPos+100,yPos+100);
Paint myPaint = new Paint();
myPaint.setColor(Color.rgb(0, 102, 255));
canvas.drawRect(player, myPaint);
myPaint.setColor(Color.rgb(0, 0, 0));
canvas.drawRect(xPos + 5, yPos + 5, xPos + 95, yPos +95, myPaint);
myPaint.setColor(Color.WHITE);
myPaint.setTextSize(20);
canvas.drawText("" + Math.round(ySpeed), 75, 50, myPaint);
canvas.drawText(ax +", "+ ay +", "+ az, 300, 50, myPaint);
if(gameStarted == false) {
myPaint.setTextSize(30);
myPaint.setTextAlign(Align.CENTER);
canvas.drawText("Touch screen to begin!", canvas.getWidth()/2, canvas.getHeight()/2, myPaint);
}
int r = Math.round(rand.nextInt(canvas.getHeight() - 150));
if(needsRand == true) {
blockY = r;
myPaint.setColor(Color.rgb(rand.nextInt(255), rand.nextInt(255), rand.nextInt(255)));
}
//if(isAlive == true) {
Rect block = new Rect(blockX,blockY,(blockX+100),(blockY+100)); // left, top, right, bottom
myPaint.setColor(Color.WHITE);
canvas.drawRect(block, myPaint);
blockX--;
//}
if(player.intersect(leftWall)) {
xSpeed = Math.abs(xSpeed);
} else if(player.intersect(rightWall)) {
xSpeed = -(xSpeed);
} else if(player.intersect(floor)) {
yPos = canvas.getHeight() - 150;
ySpeed = -(ySpeed)*.75;
} else if(player.intersect(ceiling)) {
ySpeed = Math.abs(ySpeed);
} else if(player.intersect(block)) {
ySpeed = 0;
}
if(blockX <= -100) {
blockX = canvas.getWidth() + 100;
needsRand = true;
} else {
needsRand = false;
}
canvas.drawText("" + r, 300, 100, myPaint);
physics();
invalidate();
}
@覆盖
受保护的void onDraw(画布){
//TODO自动生成的方法存根
super.onDraw(帆布);
leftWall=newrect(-100,0,0,canvas.getHeight());
rightWall=new Rect(canvas.getWidth(),0,(canvas.getWidth()+100),canvas.getHeight());
floor=new Rect(0,canvas.getHeight(),canvas.getWidth(),(canvas.getHeight()+100));
天花板=新矩形(0,-100,canvas.getWidth(),0);
加速计读取器acc=新加速计读取器();
浮动ax=根据getValueX();
浮动ay=根据getValueY();
浮点数az=根据getValueZ();
canvas.drawColor(Color.rgb(红、绿、蓝));//0或51?,51,102
Rect player=新的Rect(xPos、yPos、xPos+100、yPos+100);
绘制myPaint=新绘制();
设置颜色(Color.rgb(0,102,255));
canvas.drawRect(播放器,myPaint);
设置颜色(Color.rgb(0,0,0));
drawRect(xPos+5、yPos+5、xPos+95、yPos+95、myPaint);
myPaint.setColor(颜色:白色);
myPaint.setTextSize(20);
画布.drawText(“+Math.round(ySpeed),75,50,myPaint);
画布.绘图文本(ax+“,“+ay+”,“+az,300,50,myPaint);
如果(gameStarted==false){
myPaint.setTextSize(30);
myPaint.setTextAlign(Align.CENTER);
canvas.drawText(“触摸屏开始!”,canvas.getWidth()/2,canvas.getHeight()/2,myPaint);
}
int r=Math.round(rand.nextInt(canvas.getHeight()-150));
if(needsRand==true){
blockY=r;
myPaint.setColor(Color.rgb(rand.nextInt(255)、rand.nextInt(255)、rand.nextInt(255));
}
//如果(isAlive==true){
Rect block=new Rect(blockX,blockY,(blockX+100),(blockY+100));//左、上、右、下
myPaint.setColor(颜色:白色);
canvas.drawRect(块,myPaint);
blockX--;
//}
if(玩家相交(左墙)){
xSpeed=Math.abs(xSpeed);
}如果(player.intersect(rightWall)){
xSpeed=-(xSpeed);
}否则,如果(球员交叉(地板)){
yPos=canvas.getHeight()-150;
ySpeed=-(ySpeed)*.75;
}否则,如果(播放器相交(天花板)){
ySpeed=Math.abs(ySpeed);
}否则如果(玩家交叉(拦网)){
y速度=0;
}
如果(blockX我觉得你使用的方法很奇怪。你实际上在绘图类中提取了加速计值。就像,你创建了加速计类的一个实例,然后立即提取他的值(而不等待真正的加速计实际检索值)然后试着画出来。这可能是你的问题所在,你画出开始的加速计值,当它们改变时不刷新它们。我不知道你剩下的代码是什么,但是如果你多次调用onDraw,你只需要创建一个新的类实例,你从中提取的值也没有时间被修改传感器读数
您最好使用一个push方法,在Accelerator类中实现一个侦听器接口,这样每当检索到加速度的新值时,就会通知drawing类并获取新值,然后绘制它们
编辑:我强烈建议您对加速计使用更快的延迟(最快的可以)。请记住,加速计是所有加速计中耗电更少的一个。MyService.java
public class MyService extends Service implements SensorEventListener {
public void onCreate() {
super.onCreate();
sm = (SensorManager) getSystemService(SENSOR_SERVICE);
sm.unregisterListener(this);
sm.registerListener(this,sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),SensorManager.SENSOR_DELAY_FASTEST);
}
public void onDestroy() {
super.onDestroy();
sm.unregisterListener(this);
}
// Creates a new binder
@Override
public IBinder onBind(Intent intent) {
return new MyBinder<MyService>(this);
}
public void onSensorChanged(SensorEvent event) {
ax=event.values[0];
ay=event.values[1];
az=event.values[2];
for (Listener listener : listeners) listener.handleNewAccelValue(this);
}
public interface Listener {
// Actions to take when a new position has been added to the model
void handleNewAccelValue(MyService sender);
}
// List of all the ongoing listeners bound to the Model
private List<Listener> listeners = new ArrayList<Listener>();
// Binds a listener from the View to the Model
public void addListener(Listener listener) {
this.listeners.add(listener);
}
// Removes a listener from the View to the Model
public void removeListener(Listener listener) {
this.listeners.remove(listener);
}
公共类MyService扩展服务实现SensorEventListener{
public void onCreate(){
super.onCreate();
sm=(传感器管理器)getSystemService(传感器服务);
sm.取消注册侦听器(此);
sm.registerListener(此,sm.getDefaultSensor(Sensor.TYPE\u Accelerator),SensorManager.Sensor\u DELAY\u最快);
}
公共空间{
super.ondestory();
sm.取消注册侦听器(此);
}
//创建一个新的活页夹
@凌驾
公共IBinder onBind(意向){
返回新的MyBinder(此);
}
传感器更改时的公共无效(传感器事件){
ax=事件值[0];
ay=事件值[1];
az=事件值[2];
for(Listener-Listener:listeners)Listener.handleNewAccelValue(this);
}
公共接口侦听器{
//将新职位添加到模型时要采取的操作
void handleNewAccelValue(MyService发送方);
}
//绑定到模型的所有正在进行的侦听器的列表
私有列表侦听器=新的ArrayList();
//将侦听器从视图绑定到模型
公共void addListener(侦听器侦听器){
this.listeners.add(listener);
}
//将侦听器从视图中删除到模型
公共void RemovelListener(侦听器侦听器){
this.listeners.remove(listener);
}
YourActivity.java
public class YourActivity implements MyService.Listener{
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
// Binds to the created service
doBindService();
}
// Creating the controller and the model
private ServiceComputeFinal controller;
// Creates the connection with a MyService service
ServiceConnection connection = new ServiceConnection(){
public void onServiceConnected(ComponentName name, IBinder binder) {
controller = ((MyBinder<MyService>) binder).getService();
}
public void onServiceDisconnected(ComponentName name) {
controller = null;
}
};
// Binds to the created service
void doBindService(){
bindService(new Intent(this, MyService.class), connection, Context.BIND_AUTO_CREATE);
}
// And finally recover the accelerometer data that interest you
public void handleNewAccelValue(MyService sender){
DO_WHATEVER_YOU_WANT_WITH(controller.getAx());
}
public类YourActivity实现MyService.Listener{
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
//绑定到创建的服务
doBindService();
}
//创建控制器和模型
专用服务计算机终端控制器;
//创建与MyService服务的连接
ServiceConnection连接=新的ServiceConnection(){
服务连接上的公共void(组件名称,IBinder绑定器){
控制器=((MyBinder)binder).getService();
}
ServiceDisconnected上的公共无效(组件名称){
控制器=空;