Android game file read-write class and drawing class design

Source: Internet
Author: User
Tags readfile

Android game file read-write class and drawing class design

1. Basic knowledge:


A. FileInputStream

http://developer.android.com/reference/java/io/FileInputStream.html
B. FileOutputStream
http://developer.android.com/reference/java/io/FileOutputStream.html
C. Environment.getexternalstoragedirectory ()
http://developer.android.com/reference/android/os/Environment.html#getExternalStorageDirectory()
D. Bitmap
http://developer.android.com/reference/android/graphics/Bitmap.html
E. Canvas
http://developer.android.com/reference/android/graphics/Canvas.html
F. Paint
http://developer.android.com/reference/android/graphics/Paint.html
G. Rect
http://developer.android.com/reference/android/graphics/Rect.html


2, design file read-write interface FileIO and file read-write class Androidfileio
Package Com.badlogic.androidgames.framework;import Java.io.ioexception;import Java.io.inputstream;import Java.io.outputstream;public interface FileIO {public    inputstream readasset (String fileName) throws IOException;    Public InputStream readFile (String fileName) throws IOException;    Public OutputStream WriteFile (String fileName) throws IOException;}


Package Com.badlogic.androidgames.framework.impl;import Java.io.file;import Java.io.fileinputstream;import Java.io.fileoutputstream;import Java.io.ioexception;import Java.io.inputstream;import Java.io.OutputStream;import Android.content.res.assetmanager;import Android.os.environment;import Com.badlogic.androidgames.framework.FileIO    ;p Ublic 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 (exte    Rnalstoragepath + fileName); } @Override Public OutputStream WriteFile (String fileName) throws Ioexception {return new FileOutputStream (Externalstoragepath + fileName); }}


3. Design pixel interface Pixmap, drawing interface graphics, pixel class Androidpixmap and drawing class Androidgraphics
Package Com.badlogic.androidgames.framework;import Com.badlogic.androidgames.framework.Graphics.PixmapFormat; Public interface Pixmap {public    int getwidth ();    public int getheight ();    Public Pixmapformat GetFormat ();    public void Dispose ();}


Package Com.badlogic.androidgames.framework;public interface Graphics {public    static enum Pixmapformat {        ARGB8888, ARGB4444, RGB565    } public    pixmap newpixmap (String fileName, pixmapformat format);    public void Clear (int color);    public void Drawpixel (int x, int y, int color);    public void DrawLine (int x, int y, int x2, int y2, int color);    public void DrawRect (int x, int y, int width, int height, int color);    public void Drawpixmap (pixmap pixmap, int x, int y, int srcx, int srcy,            int srcwidth, int srcheight);    public void Drawpixmap (pixmap pixmap, int x, int y);    public int getwidth ();    public int getheight ();}


Package Com.badlogic.androidgames.framework.impl;import Android.graphics.bitmap;import Com.badlogic.androidgames.framework.graphics.pixmapformat;import Com.badlogic.androidgames.framework.Pixmap; public class Androidpixmap implements Pixmap {    Bitmap Bitmap;    Pixmapformat format;        Public Androidpixmap (Bitmap Bitmap, Pixmapformat format) {        this.bitmap = Bitmap;        This.format = format;    }    @Override public    int getwidth () {        return bitmap.getwidth ();    }    @Override public    int getheight () {        return bitmap.getheight ();    }    @Override public    Pixmapformat GetFormat () {        return format;    }    @Override public    void Dispose () {        bitmap.recycle ();    }      }


Package Com.badlogic.androidgames.framework.impl;import Java.io.ioexception;import Java.io.inputstream;import Android.content.res.assetmanager;import Android.graphics.bitmap;import Android.graphics.bitmap.config;import Android.graphics.bitmapfactory;import Android.graphics.bitmapfactory.options;import Android.graphics.Canvas; Import Android.graphics.paint;import Android.graphics.paint.style;import Android.graphics.rect;import Com.badlogic.androidgames.framework.graphics;import Com.badlogic.androidgames.framework.pixmap;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;        This.canvas = new canvas (frameBuffer);    This.paint = new paint (); } @Override Public Pixmap newpixmap (String fileName, PixmapFormat format) {config config = null;        if (format = = pixmapformat.rgb565) config = config.rgb_565;        else if (format = = pixmapformat.argb4444) 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 '" + F        Ilename + "'");                } 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.argb4444;        else format = pixmapformat.argb8888;    return new Androidpixmap (bitmap, format); } @Override public void clear (int color) {Canvas.drawrgb (color & 0xff0000) >>, (Color & 0x    FF00) >> 8, (color & 0xff));        } @Override public void drawpixel (int x, int y, int color) {paint.setcolor (color);    Canvas.drawpoint (x, y, paint);        } @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 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 + width-1, paint); } @Override PUBlic void Drawpixmap (pixmap pixmap, int x, int y, int srcx, int srcy, int srcwidth, int srcheight) {src        Rect.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, NULL); } @Override public void Drawpixmap (pixmap pixmap, int x, int y) {canvas.drawbitmap ((androidpixmap) Pixma    p). Bitmap, x, y, null);    } @Override public int getwidth () {return framebuffer.getwidth ();    } @Override public int getheight () {return framebuffer.getheight (); }}


4. Another drawing class glgraphics based on OpenGL ES (previous drawing class Androidgraphics is based on pixel drawing)
Package Com.badlogic.androidgames.framework.impl;import Javax.microedition.khronos.opengles.gl10;import Android.opengl.glsurfaceview;public class Glgraphics {    Glsurfaceview glview;    GL10 GL;        Glgraphics (Glsurfaceview glview) {        this.glview = Glview;    }        Public GL10 GETGL () {        return GL;    }        void Setgl (GL10 gl) {        THIS.GL = GL;    }        public int getwidth () {        return glview.getwidth ();    }        public int getheight () {        return glview.getheight ();    }}



This paper briefly describes the basic knowledge of "beginning-android-games" books and the design analysis of related classes,
Books and source code:http://download.csdn.net/detail/yangzhenping/8420261
The class implementation in this article is from "Beginning-android-games\ch07-gl-basics"

Android game file read-write class and drawing class design

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.