Android中贪吃蛇游戏的学习(四)

来源:互联网 发布:移动网络屏蔽360云盘 编辑:程序博客网 时间:2024/06/05 17:34

Android中贪吃蛇游戏的学习(四)

文章分类:移动开发

Java代码 复制代码
  1. package com.easyway.dev.android.snake;   
  2.   
  3. import android.content.Context;   
  4. import android.content.res.TypedArray;   
  5. import android.graphics.Bitmap;   
  6. import android.graphics.Canvas;   
  7. import android.graphics.Paint;   
  8. import android.graphics.drawable.Drawable;   
  9. import android.util.AttributeSet;   
  10. import android.view.View;   
  11.   
  12.   
  13. /**  
  14.  * Android 平台裡,使用者介面都是透过 ViewGroup 或 View 类别来显示。  
  15.  * ViewGroup 和 View 是 Android 平台上最基本的使用者介面表达单元。我  
  16.  * 们可以透过程式直接呼叫的方法,调用描绘使用者介面,将萤幕上显示的介面元  
  17.  * 素,与构成应用程式主体的程式逻辑,溷合在一起编写。或是,也可以将介面显示  
  18.  * 与程式逻辑分离,照着 Android 提供的这个较优雅的方式,使用 XML 描述档,  
  19.  * 来描述介面元件的组织。  
  20.  *   
  21.  * 在 Android 系统中,我们使用 XML 来定义 UI。但是有些稍微有经验的开发者可能会有疑问:   
  22.  *「用 XML 来描述介面固然方便,但是对于手机程式来说,直接用 XML 档桉是不是太占空间了?」。   
  23.  *没错,如果 Android 是直接使用 XML 来储存介面描述到手机上的话,一定会佔用比起现在大的多  
  24.  *的档桉空间。解决的方法是Android 并不直接使用 XML 档桉,而是透过 Android 开发工具,  
  25.  *自动将 XML 描述档转换成资源档桉。一旦应用程式要操作某个介面元件,或是使用任何种类的资源  
  26.  *(字串、图片、图示、音效...),都使用索引来查询。   
  27.  *  
  28.  *  
  29.  *伟大的创意少之又少,多数时候只是一些小改进。小的改进也是好的。  
  30.  *  
  31.  *  
  32.  * TileView: a View-variant designed for handling arrays of "icons" or other  
  33.  * drawables.  
  34.  *   
  35.  */  
  36. public class TileView extends View {   
  37.   
  38.     /**  
  39.      * Parameters controlling the size of the tiles and their range within view.  
  40.      * Width/Height are in pixels, and Drawables will be scaled to fit to these  
  41.      * dimensions. X/Y Tile Counts are the number of tiles that will be drawn.  
  42.      */  
  43.   
  44.     protected static int mTileSize;   
  45.   
  46.     protected static int mXTileCount;   
  47.     protected static int mYTileCount;   
  48.   
  49.     private static int mXOffset;   
  50.     private static int mYOffset;   
  51.   
  52.   
  53.     /**  
  54.      *   
  55.      * A hash that maps integer handles specified by the subclasser to the  
  56.      * drawable that will be used for that reference  
  57.      */  
  58.     private Bitmap[] mTileArray;    
  59.   
  60.     /**  
  61.      * 声明用来存放绘画图像的x,y轴的位置的数组  
  62.      * A two-dimensional array of integers in which the number represents the  
  63.      * index of the tile that should be drawn at that locations  
  64.      */  
  65.     private int[][] mTileGrid;   
  66.   
  67.     private final Paint mPaint = new Paint();   
  68.   
  69.     public TileView(Context context, AttributeSet attrs, int defStyle) {   
  70.         super(context, attrs, defStyle);   
  71.   
  72.         TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.TileView);   
  73.   
  74.         mTileSize = a.getInt(R.styleable.TileView_tileSize, 12);   
  75.            
  76.         a.recycle();   
  77.     }   
  78.   
  79.     public TileView(Context context, AttributeSet attrs) {   
  80.         super(context, attrs);   
  81.   
  82.         TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.TileView);   
  83.   
  84.         mTileSize = a.getInt(R.styleable.TileView_tileSize, 12);   
  85.            
  86.         a.recycle();   
  87.     }   
  88.   
  89.        
  90.        
  91.     /**  
  92.      * Rests the internal array of Bitmaps used for drawing tiles, and  
  93.      * sets the maximum index of tiles to be inserted  
  94.      *   
  95.      * @param tilecount  
  96.      */  
  97.        
  98.     public void resetTiles(int tilecount) {   
  99.         mTileArray = new Bitmap[tilecount];   
  100.     }   
  101.   
  102.   
  103.     @Override  
  104.     protected void onSizeChanged(int w, int h, int oldw, int oldh) {   
  105.         mXTileCount = (int) Math.floor(w / mTileSize);   
  106.         mYTileCount = (int) Math.floor(h / mTileSize);   
  107.   
  108.         mXOffset = ((w - (mTileSize * mXTileCount)) / 2);   
  109.         mYOffset = ((h - (mTileSize * mYTileCount)) / 2);   
  110.   
  111.         mTileGrid = new int[mXTileCount][mYTileCount];   
  112.         clearTiles();   
  113.     }   
  114.   
  115.     /**  
  116.      * Function to set the specified Drawable as the tile for a particular  
  117.      * integer key.  
  118.      *   
  119.      * @param key  
  120.      * @param tile  
  121.      */  
  122.     public void loadTile(int key, Drawable tile) {   
  123.         Bitmap bitmap = Bitmap.createBitmap(mTileSize, mTileSize, Bitmap.Config.ARGB_8888);   
  124.         Canvas canvas = new Canvas(bitmap);   
  125.         tile.setBounds(00, mTileSize, mTileSize);   
  126.         tile.draw(canvas);   
  127.            
  128.         mTileArray[key] = bitmap;   
  129.     }   
  130.   
  131.     /**  
  132.      * Resets all tiles to 0 (empty)  
  133.      *   
  134.      */  
  135.     public void clearTiles() {   
  136.         for (int x = 0; x < mXTileCount; x++) {   
  137.             for (int y = 0; y < mYTileCount; y++) {   
  138.                 setTile(0, x, y);   
  139.             }   
  140.         }   
  141.     }   
  142.   
  143.     /**  
  144.      * Used to indicate that a particular tile (set with loadTile and referenced  
  145.      * by an integer) should be drawn at the given x/y coordinates during the  
  146.      * next invalidate/draw cycle.  
  147.      *   
  148.      * @param tileindex 图片的索引  
  149.      * @param x  x轴的位置   
  150.      * @param y  y轴的位置   
  151.      */  
  152.     public void setTile(int tileindex, int x, int y) {   
  153.         mTileGrid[x][y] = tileindex;   
  154.     }   
  155.   
  156.     /**  
  157.      * 重写VIEW 类里面的方法。 把界线画出。  
  158.      *   
  159.      * 地图其实就是由图片数组拼直面成的。 面图片又是通过他的图片索引找到,并  
  160.      * 在mTileGrid[x][y],获取他们的位置索引来确定图片的位置。 这样在一个  
  161.      * 手机的页面就形成了,  
  162.      *   
  163.      */  
  164.     @Override  
  165.     public void onDraw(Canvas canvas) {   
  166.         super.onDraw(canvas);   
  167.         for (int x = 0; x < mXTileCount; x += 1) {   
  168.             for (int y = 0; y < mYTileCount; y += 1) {   
  169.                 if (mTileGrid[x][y] > 0) {   
  170.                     canvas.drawBitmap(mTileArray[mTileGrid[x][y]],    
  171.                             mXOffset + x * mTileSize,   
  172.                             mYOffset + y * mTileSize,   
  173.                             mPaint);   
  174.                 }   
  175.             }   
  176.         }   
  177.   
  178.     }   
  179.   
  180. }