我目前正在使用此项目作为模板: http://www.droidnova.com/playing-with-graphics-in-android-part-vii,220.html
我的背景和飞机装在屏幕上。我在主页上添加了一个传感器管理器。
private SensorManager sensorManager;
/**
* Method called on application start.
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(new Panel(this));
sensorManager=(SensorManager)getSystemService(SENSOR_SERVICE);
// add listener. The listener will be HelloAndroid (this) class
sensorManager.registerListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_NORMAL);
}
public void onAccuracyChanged(Sensor sensor,int accuracy){
}
public void onSensorChanged(SensorEvent event){
// check sensor type
if(event.sensor.getType()==Sensor.TYPE_ACCELEROMETER){
// assign directions
float x=event.values[0];
float y=event.values[1];
float z=event.values[2];
//Draw my plane here..........
}
}
我想做的是用新的坐标x和y移动我的飞机。我是否需要将传感器管理器放在面板类中,还是可以从我的主类中进行?这是我的小组课程:
public class Panel extends SurfaceView implements SurfaceHolder.Callback {
private ObitBlastThread _thread;
private ArrayList<Graphic> _graphics = new ArrayList<Graphic>();
private Graphic _currentGraphic;
private SoundPool _soundPool;
private Map<Integer, Bitmap> _bitmapCache = new HashMap<Integer, Bitmap>();
public Panel(Context context) {
super(context);
fillBitmapCache();
_soundPool = new SoundPool(16, AudioManager.STREAM_MUSIC, 100);
//_playbackFile = _soundPool.load(getContext(), R.raw.explosion, 0);
getHolder().addCallback(this);
_thread = new ObitBlastThread(this);
setFocusable(true);
}
private void fillBitmapCache() {
_bitmapCache.put(R.drawable.background, BitmapFactory.decodeResource(getResources(), R.drawable.background));
_bitmapCache.put(R.drawable.mainairplane, BitmapFactory.decodeResource(getResources(), R.drawable.mainairplane));
}
@Override
public void onDraw(Canvas canvas) {
// draw the background
canvas.drawBitmap(_bitmapCache.get(R.drawable.background), 0, 0, null);
canvas.drawBitmap(_bitmapCache.get(R.drawable.mainairplane), 100, 225, null);
Bitmap bitmap;
Graphic.Coordinates coords;
// draw the normal items
for (Graphic graphic : _graphics) {
bitmap = graphic.getBitmap();
coords = graphic.getCoordinates();
canvas.drawBitmap(bitmap, coords.getX(), coords.getY(), null);
}
// draw current graphic at last...
if (_currentGraphic != null) {
bitmap = _currentGraphic.getBitmap();
coords = _currentGraphic.getCoordinates();
canvas.drawBitmap(bitmap, coords.getX(), coords.getY(), null);
}
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
if (!_thread.isAlive()) {
_thread = new ObitBlastThread(this);
}
_thread.setRunning(true);
_thread.start();
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
boolean retry = true;
_thread.setRunning(false);
while (retry) {
try {
_thread.join();
retry = false;
} catch (InterruptedException e) {
// we will try it again and again...
}
}
Log.i("thread", "Thread terminated...");
}
}