关于用户事件的处理是比较复杂的一个部分,在上一篇过后,剩下的关于Audio,Graphics,FileIO的部分就显得比较简单了
首先来看AndroidFileIO,这里的File主要存在于2个地方,一个是SD卡上,另一个是assets文件夹中
public class AndroidFileIO implements FileIO { AssetManager assets; String externalStoragePath; public AndroidFileIO(AssetManager assets){ this.assets=assets; this.externalStoragePath=Environment.getExternalStorageDirectory().getAbsolutePath()+File.separator; } @Override public InputStream readAsset(String fileName) throws IOException { return assets.open(fileName); } @Override public InputStream readFile(String fileName) throws IOException { return new FileInputStream(externalStoragePath+fileName); } @Override public OutputStream writeFile(String fileName) throws IOException { return new FileOutputStream(externalStoragePath+fileName); } }
再看AndroidGraphics的实现 主要关系到2个类,AndroidGraphics和AndroidPixmap,他们分别是之前给出抽象中Graphics和Pixmap的实现
public class AndroidPixmap implements Pixmap { Bitmap bitmap; PixmapFormat format; public AndroidPixmap(Bitmap bitmap, PixmapFormat format){ this.bitmap=bitmap; this.format=format; } @Override public void dispose() { bitmap.recycle(); } @Override public PixmapFormat getFormat() { return format; } @Override public int getWidth() { return bitmap.getWidth(); } @Override public int getHeight() { return bitmap.getHeight(); } }
public class AndroidGraphics implements Graphics { AssetManager assets; Bitmap frameBuffer; Canvas canvas; Paint paint; Rect srcRect = new Rect(); Rect dstRect = new Rect(); public AndroidGraphics(AssetManager assets, Bitmap frameBuffer) { this.assets = assets; this.frameBuffer = frameBuffer; canvas = new Canvas(frameBuffer); paint = new Paint(); } @Override public void clear(int color) { canvas.drawRGB((color & 0xff0000) >> 16, (color & 0xff00) >> 8, (color & 0xff)); } @Override public void drawLine(int x, int y, int x2, int y2, int color) { paint.setColor(color); canvas.drawLine(x, y, x2, y2, paint); } @Override public void drawPixel(int x, int y, int color) { paint.setColor(color); canvas.drawPoint(x, y, paint); } @Override public void drawPixmap(Pixmap pixmap, int x, int y, int srcX, int srcY, int srcWidth, int srcHeight) { srcRect.left=srcX; srcRect.top=srcY; srcRect.right=srcX+srcWidth-1; srcRect.bottom=srcY+srcHeight-1; dstRect.left=x; dstRect.top=y; dstRect.right=x+srcWidth-1; dstRect.bottom=y+srcHeight-1; canvas.drawBitmap(((AndroidPixmap)pixmap).bitmap, srcRect, dstRect, paint); } @Override public void drawPixmap(Pixmap pixmap, int x, int y) { canvas.drawBitmap(((AndroidPixmap)pixmap).bitmap, x, y, paint); } @Override public void drawRect(int x, int y, int width, int height, int color) { paint.setColor(color); paint.setStyle(Style.FILL); canvas.drawRect(x, y, x + width - 1, y + height - 1, paint); } @Override public int getHeight() { return frameBuffer.getHeight(); } @Override public int getWidth() { return frameBuffer.getWidth(); } @Override public Pixmap newPixmap(String fileName, PixmapFormat format) { Config config = null; if (format == PixmapFormat.RGB565) { config = Config.RGB_565; } else if (format == PixmapFormat.ARGB84444) { config = Config.ARGB_4444; } else { config = Config.ARGB_8888; } Options options = new Options(); options.inPreferredConfig = config; InputStream in = null; Bitmap bitmap = null; try { in = assets.open(fileName); bitmap = BitmapFactory.decodeStream(in); if (bitmap == null) { throw new RuntimeException("Couldn't load bitmap from asset '" + fileName + "'"); } } catch (IOException e) { throw new RuntimeException("Couldn't load bitmap from asset '" + fileName + "'"); } finally { if (in != null) { try { in.close(); } catch (IOException e) { } } } if (bitmap.getConfig() == Config.RGB_565) { format = PixmapFormat.RGB565; } else if (bitmap.getConfig() == Config.ARGB_4444) { format = PixmapFormat.ARGB84444; } else { format = PixmapFormat.ARGB8888; } return new AndroidPixmap(bitmap, format); } }