だから私は仕事をすることができない以下のコードを持っています。あるクラス (AccelerometerReader) で携帯電話の加速度計の値を読み取り、別のクラス (MyGame) でそれらの値を呼び出して、それらの値を画面に出力しようとしています。コード内のすべてが問題ないように見えますが、出力する値がすべて 0.0 と表示され、変更されないことを除いて、値は得られません。実際、私の電話にも加速度計が搭載されています。どんな助けでも大歓迎です!
ありがとう、オーウェン
AccelerometerReader クラス
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;
}
}
MyGame クラスの内部
@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();
}