一、Activity生命周期
游戏开发时我们只需要重载 onCreate(), onResume(), 和onPause() 方法,因为无论如何onResume(), 和onPause() 都会调用。当onPause() 之后,系统可能由于内存过低杀掉该activity,然后 onStop() 和onDestroy()就不会被执行,而onStart()要在onStop()执行了才会被调用,onpause()之后唤醒activity只会调用onResume().
1)In onCreate(), we set up our window and UI component that we
render to and receive input from.
2)In onResume(), we (re)start our main loop thread .
3)In onPause(), we simply pause our main loop thread, and if
Activity.isFinishing() returns true, we also save any state we want
to persist to disk.
二、事件处理
1)多点触控
比单点触控多的新事件:
1、MotionEvent.ACTION_POINTER_DOWN: This event happens for any additional finger that
touches the screen after the first finger touches. The first finger will still produce a
MotionEvent.ACTION_DOWN event.
2、MotionEvent.ACTION_POINTER_UP: This is analogous the previous action. This gets
fired when a finger is lifted up from the screen and more than one finger is touching
the screen. The last finger on the screen to go up will produce a
MotionEvent.ACTION_UP event. This finger doesn’t necessarily have to be the first
finger that touched the screen.
三、按键事件
public boolean onKey(View view, int keyCode, KeyEvent event)
keycode:代表手机上按键的唯一标示符,例如 KeyCode.KEYCODE_A代表按了A键
keyEvent:它和MotionEvent类似,有两个重要方法
KeyEvent.getAction(): 这个方法返回 KeyEvent.ACTION_DOWN,
KeyEvent.getUnicodeChar() :将点击事件当做字符处理
注:获取按键事件,view必须获取了focus,
View.setFocusableInTouchMode(true);
View.requestFocus();
四、加速度感应器
1、通过Context接口获取感应器服务
SensorManager manager = (SensorManager)context.getSystemService(Context.SENSOR_SERVICE);
2、检测是否含有加速度感应器(可选,因为所有的android设备都装有加速度感应器)
boolean hasAccel = manager.getSensorList(Sensor.TYPE_ACCELEROMETER).size() > 0;
3、注册监听器
SensorManager.SENSOR_DELAY_GAME);
五、音效处理
context.setVolumeControlStream(AudioManager.STREAM_MUSIC); //设置音量键为调节音量大小
用soundpool播放实时音效
1、SoundPool soundPool = new SoundPool(20, AudioManager.STREAM_MUSIC, 0);
2、把音效文件加载到内存
AssetManager assetManager = getAssets();
int explosionId = soundPool.load(descriptor, 1);
3、播放
soundPool.play(explosionId, 1.0f, 1.0f, 0, 0, 1);
4、当不用的时候记得释放内存
soundPool.unload(explosionId);
SoundPool.release();//释放所有SoundPool所用的资源
用MediaPlayer播放背景音乐
1、MediaPlayer mediaPlayer = new MediaPlayer();
2、AssetManager assetManager = getAssets();
AssetFileDescriptor descriptor = assetManager.openFd("music.ogg");
descriptor.getLength());
3、 打开音乐文件并检查是否可播放
mediaPlayer.prepare();
4、播放
mediaPlayer.start();
mediaPlayer.pause();
mediaPlayer.stop(); //再次播放需要先mediaPlayer.prepare(),再mediaPlayer.start()
5释放资源
mediaPlayer.release();
MediaPlayer占用相当大的资源,所以只用于播放背景音乐
六、2D图像处理
1、加载图片
AssetManager assetManager = context.getAssets();
Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
inputStream.close();
2、绘制
Canvas.drawBitmap(Bitmap bitmap, float topLeftX, float topLeftY, Paint paint);
或Canvas.drawBitmap(Bitmap bitmap, Rect src, Rect dst, Paint paint);
2、释放图片资源
Bitmap.recycle();
SurfaceView
SurfaceView比View的好处是可以在一个单独的线程中处理游戏逻辑,从而避免阻塞UI线程
1、在构造函数中获取SurfaceHolder
SurfaceHolder holder = surfaceView.getHolder();
2、绘制
holder.unlockCanvasAndPost(canvas);