Android的SDK中Snake代码分析(二)——TileView
来源:互联网 发布:网络歌手名单大全 编辑:程序博客网 时间:2024/06/09 20:46
/*
* Copyright (C) 2007 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.example.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;
/**
* 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;
/**
* 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];
}
//在游戏还未正式开始前,首先要做一些初始化工作,在View第一次加载时会首先调用onSizeChanged
@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;
}
//onDraw在游戏进行过程中,会不断地被调用,从而界面不断被更新
@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);
}
}
}
}
}
- Android的SDK中Snake代码分析(二)——TileView
- Android的SDK中Snake代码分析(一)——Snake
- Android的SDK中Snake代码分析(三)——SnakeView
- 手势操作的Snake游戏_游戏视图分析之TileView
- API Demo Snake代码分析二 onSaveInstanceState和onRestoreInstanceState分析
- OpenCv 中Snake程序的例子(二)
- 【贪吃蛇—Java程序员写Android游戏】系列 1.Android SDK Sample-Snake详解
- API Demo Snake代码分析三 程序架构的分析
- Silverlight中实现类似Telerik的TileView控件效果
- API Demo Snake代码分析一 FrameLayout新的认识
- Android SDK 实例代码分析---Accelerometer Play(二)
- android samples 之 Snake分析
- 我的第一个游戏—SNAKE
- 主动轮廓线模型Snake模型简介&openCV中cvSnakeImage()函数代码分析
- Android SDK实例之Snake游戏深入解析(一)
- android 项目实训—贪吃蛇Snake(一)
- GVF Snake 学习的分析总结
- Android 基础(二)、SDK中重要的pacakages(包)
- 5月刊推荐:怎样才是真正的架构师
- jquery代码优化笔记
- Android的SDK中Snake代码分析(一)——Snake
- 我生活在怎样的世界
- FreeWheel创始人/CTO于晶纯访谈:具备大局观方能洞若观火
- Android的SDK中Snake代码分析(二)——TileView
- 字符编码(转)
- Android的SDK中Snake代码分析(三)——SnakeView
- PHP中的session有效期问题
- CRF++中文分词使用指南
- MFC智能设备全屏显示
- MFC智能设备全屏显示
- Hibernate 三种状态的区分,以及save,update,saveOrUpdate,merge等的使用
- Windows CE API机制初探