android 游戏开发三:涉及到的API简单介绍
一、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 (discussed in the last
chapter).
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)多点触控
public boolean onTouch(View v, MotionEvent event) {
int action = event.getAction() & MotionEvent.ACTION_MASK;//获取点击事件的种类
int pointerIndex = (event.getAction() & MotionEvent.ACTION_POINTER_ID_MASK) >> MotionEvent.ACTION_POINTER_ID_SHIFT;//获取点击事件的索引
int pointerId = event.getPointerId(pointerIndex);//获取点击事件的ID
switch (action) {
case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_POINTER_DOWN:
touched[pointerId] = true;
x[pointerId] = (int)event.getX(pointerIndex);
y[pointerId] = (int)event.getY(pointerIndex);
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_POINTER_UP:
case MotionEvent.ACTION_CANCEL:
touched[pointerId] = false;
x[pointerId] = (int)event.getX(pointerIndex);
y[pointerId] = (int)event.getY(pointerIndex);
break;
比单点触控多的新事件:
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 易做图ogous 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.
利用单点触控处理多点触控
For this to happen, the merged events have to have the
same type. In reality this will only happen for the MotionEvent.ACTION_MOVE event, so we
only have to deal with this fact when processing said event type. To check how many
events are contained in a single MotionEvent, we use the
MotionEvent.getPointerCount() method, which tells us for how many fingers the
MotionEvent contains coordinates for. We then can fetch the pointer identifier and
coordinates for the pointer indices 0 to MotionEvent.getPointerCount() – 1 via the
MotionEvent.getX(), MotionEvent.getY(), and MotionEvent.getPointerId() methods.
case MotionEvent.ACTION_MOVE:
int pointerCount = event.getPointerCount();//获取点击事件的数量
for (int i = 0; i < pointerCount; i++) {
pointerIndex = i;
pointerId = event.getPointerId(pointerIndex);
x[pointerId] = (int)event.getX(pointerIndex);
y[pointerId] = (int
三、按键事件
public boolean onKey(View view, int keyCode, KeyEvent event)
keycode:代表手机上按键的唯一标示符,例如KeyCode.KEYCODE_A代表按了A键
keyEvent:它和MotionEvent类似,有两个重要方法
KeyEvent.getAction(): 这个方法返回KeyEvent.ACTION_DOWN,
KeyEvent.ACTION_UP, and KeyEvent.ACTION_MULTIPLE.
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、注册易做图
Sensor sensor = manager.getSensorList(Sensor.TYPE_ACCELEROMETER).get(0);
boolean success = manager.registerListener(listener, sensor,
SensorManager.SENSOR_DELAY_GAME);
五、音效处理
context.setVolumeControlStream(AudioManager.STREAM_MUSIC); //设置音量键为调节音量大小
用soundpool播放实时音效
1、SoundPool soundPool = new SoundPool(20, AudioManager.STREAM_MUSIC, 0);
2、把音效文件加载到内存
AssetManager assetManager = getAssets();
AssetFileDescriptor descriptor = assetManager.openFd("explosion.ogg"); //游戏开发通常把音效图片等资源放到assets中,便于用分级文件夹管理
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");
mediaPlayer.setDataSource(descriptor.getFileDescriptor(), descriptor.getStartOffset(),
descriptor.getLength());
3、 打开音乐文件并检查是否可播放
mediaPlayer.prepare();
4、播放
mediaPlayer.start();
mediaPlayer.pause();
mediaPlayer.stop(); //再次播放需要先mediaPlayer.prepare(),再mediaPlayer.start()
mediaPlayer.setLooping(true);
5释放资源
mediaPlayer.release();
MediaPlayer占用相当大的资源,所以只用于播放背
补充:移动开发 , Android ,