设为首页 加入收藏

TOP

Android 贪吃蛇源码分析(二)
2014-11-24 03:00:55 来源: 作者: 【 】 浏览:3
Tags:Android 源码 分析
ic int mTileSize;


/** x轴方向上格子的个数 */
protected static int mXTileCount;
/** y轴方向上格子的个数 */
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
* @param y
*/
public void setTile(int tileindex, int x, int y) {
mTileGrid[x][y] = tileindex;
}


/**
* 画出主界面图
*/
@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);
}
}
}


}


}


首页 上一页 1 2 下一页 尾页 2/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇Android 页面切换动画效果 下一篇Android中实现view的更新UI有两组..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·Sphinx : 高性能SQL (2025-12-24 10:18:11)
·Pandas 性能优化 - (2025-12-24 10:18:08)
·MySQL 索引 - 菜鸟教 (2025-12-24 10:18:06)
·Shell 基本运算符 - (2025-12-24 09:52:56)
·Shell 函数 | 菜鸟教 (2025-12-24 09:52:54)