Android中贪吃蛇游戏的学习(四)
package com.easyway.dev.android.snake;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.View;
/**
* Android 平台裡,使用者介面都是透过 ViewGroup 或 View 类别来显示。
* ViewGroup 和 View 是 Android 平台上最基本的使用者介面表达单元。我
* 们可以透过程式直接呼叫的方法,调用描绘使用者介面,将萤幕上显示的介面元
* 素,与构成应用程式主体的程式逻辑,溷合在一起编写。或是,也可以将介面显示
* 与程式逻辑分离,照着 Android 提供的这个较优雅的方式,使用 XML 描述档,
* 来描述介面元件的组织。
*
* 在 Android 系统中,我们使用 XML 来定义 UI。但是有些稍微有经验的开发者可能会有疑问:
*「用 XML 来描述介面固然方便,但是对于手机程式来说,直接用 XML 档桉是不是太占空间了?」。
*没错,如果 Android 是直接使用 XML 来储存介面描述到手机上的话,一定会佔用比起现在大的多
*的档桉空间。解决的方法是Android 并不直接使用 XML 档桉,而是透过 Android 开发工具,
*自动将 XML 描述档转换成资源档桉。一旦应用程式要操作某个介面元件,或是使用任何种类的资源
*(字串、图片、图示、音效...),都使用索引来查询。
*
*
*伟大的创意少之又少,多数时候只是一些小改进。小的改进也是好的。
*
*
* TileView: a View-variant designed for handling arrays of "icons" or other
* drawables.
*
*/
public class TileView extends View {
/**
* Parameters controlling the size of the tiles and their range within view.
* Width/Height are in pixels, and Drawables will be scaled to fit to these
* dimensions. X/Y Tile Counts are the number of tiles that will be drawn.
*/
protected static int mTileSize;
protected static int mXTileCount;
protected static int mYTileCount;
private static int mXOffset;
private static int mYOffset;
/**
*
* A hash that maps integer handles specified by the subclasser to the
* drawable that will be used for that reference
*/
private Bitmap[] mTileArray;
/**
* 声明用来存放绘画图像的x,y轴的位置的数组
* A two-dimensional array of integers in which the number represents the
* index of the tile that should be drawn at that locations
*/
private int[][] mTileGrid;
private final Paint mPaint = new Paint();
public TileView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.TileView);
mTileSize = a.getInt(R.styleable.TileView_tileSize, 12);
a.recycle();
}
public TileView(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.TileView);
mTileSize = a.getInt(R.styleable.TileView_tileSize, 12);
a.recycle();
}
/**
* Rests the internal array of Bitmaps used for drawing tiles, and
* sets the maximum index of tiles to be inserted
*
* @param tilecount
*/
public void resetTiles(int tilecount) {
mTileArray = new Bitmap[tilecount];
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
mXTileCount = (int) Math.floor(w / mTileSize);
mYTileCount = (int) Math.floor(h / mTileSize);
mXOffset = ((w - (mTileSize * mXTileCount)) / 2);
mYOffset = ((h - (mTileSize * mYTileCount)) / 2);
mTileGrid = new int[mXTileCount][mYTileCount];
clearTiles();
}
/**
* Function to set the specified Drawable as the tile for a particular
* integer key.
*
* @param key
* @param tile
*/
public void loadTile(int key, Drawable tile) {
Bitmap bitmap = Bitmap.createBitmap(mTileSize, mTileSize, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
tile.setBounds(0, 0, mTileSize, mTileSize);
tile.draw(canvas);
mTileArray[key] = bitmap;
}
/**
* Resets all tiles to 0 (empty)
*
*/
public void clearTiles() {
for (int x = 0; x < mXTileCount; x++) {
for (int y = 0; y < mYTileCount; y++) {
setTile(0, x, y);
}
}
}
/**
* Used to indicate that a particular tile (set with loadTile and referenced
* by an integer) should be drawn at the given x/y coordinates during the
* next invalidate/draw cycle.
*
* @param tileindex 图片的索引
* @param x x轴的位置
* @param y y轴的位置
*/
public void setTile(int tileindex, int x, int y) {
mTileGrid[x][y] = tileindex;
}
/**
* 重写VIEW 类里面的方法。 把界线画出。
*
* 地图其实就是由图片数组拼直面成的。 面图片又是通过他的图片索引找到,并
* 在mTileGrid[x][y],获取他们的位置索引来确定图片的位置。 这样在一个
* 手机的页面就形成了,
*
*/
@Override
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
for (int x = 0; x < mXTileCount; x += 1) {
for (int y = 0; y < mYTileCount; y += 1) {
if (mTileGrid[x][y] > 0) {
canvas.drawBitmap(mTileArray[mTileGrid[x][y]],
mXOffset + x * mTileSize,
mYOffset + y * mTileSize,
mPaint);
}
}
}
}
}