• 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 .
    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.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占用相当大的资源,所以只用于播放背景音乐

    六、2D图像处理

     1、加载图片

     AssetManager assetManager = context.getAssets(); 

    InputStream inputStream = assetManager.open("bob.png");

    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();  

     Unless absolutely necessary, refrain from drawing bitmaps scaled. If 
    you know their scaled size, prescale them offline or during loading 
    time. 
    Always make sure you call the Bitmap.recycle() method if you no 
    longer need a Bitmap. Otherwise you’ll get some memory leaks or run 
    low on memory. 

    SurfaceView

     SurfaceView比View的好处是可以在一个单独的线程中处理游戏逻辑,从而避免阻塞UI线程

    1、在构造函数中获取SurfaceHolder 

    SurfaceHolder holder = surfaceView.getHolder();  

    The SurfaceHolder is a wrapper around the Surface, and does some bookkeeping for 
    us. It provides us with two methods: 
     Canvas SurfaceHolder.lockCanvas(); //锁定要绘制的Surface并返回一个可用的Canvas
     SurfaceHolder.unlockAndPost(Canvas canvas);  //解锁Surface并且将我们刚才在Canvas上的绘制在屏幕上显示

    2、绘制

    Canvas canvas = holder.lockCanvas();             
       canvas.draw#ff0000;                                      

       holder.unlockCanvasAndPost(canvas);   

  • 相关阅读:
    多叉树
    PowerDesigner设置集锦(2)
    Delphi应用程序在命令行下带参数执行返回命令行提示的问题
    不允许在 '*******' 上使用扩展属性,或对象不存在
    仓库管理系统开发完成
    动态创建Frame窗体(根据类名,除T以外的字母)
    Access中的常用TSQL
    批量删除同类文件(带通配符)
    判断Access数据库中的表或查询是否存在的SQL
    序列化FastReport,重要提示少走弯路
  • 原文地址:https://www.cnblogs.com/shangdahao/p/2238438.html
Copyright © 2020-2023  润新知