FileDocCategorySizeDatePackage
TileView.javaAPI DocAndroid 1.5 API4763Wed May 06 22:41:08 BST 2009com.example.android.snake

TileView

public class TileView extends android.view.View
TileView: a View-variant designed for handling arrays of "icons" or other drawables.

Fields Summary
protected static int
mTileSize
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
mXTileCount
protected static int
mYTileCount
private static int
mXOffset
private static int
mYOffset
private android.graphics.Bitmap[]
mTileArray
A hash that maps integer handles specified by the subclasser to the drawable that will be used for that reference
private int[]
mTileGrid
A two-dimensional array of integers in which the number represents the index of the tile that should be drawn at that locations
private final android.graphics.Paint
mPaint
Constructors Summary
public TileView(android.content.Context context, android.util.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(android.content.Context context, android.util.AttributeSet attrs)

        super(context, attrs);

        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.TileView);

        mTileSize = a.getInt(R.styleable.TileView_tileSize, 12);
        
        a.recycle();
    
Methods Summary
public voidclearTiles()
Resets all tiles to 0 (empty)

        for (int x = 0; x < mXTileCount; x++) {
            for (int y = 0; y < mYTileCount; y++) {
                setTile(0, x, y);
            }
        }
    
public voidloadTile(int key, android.graphics.drawable.Drawable tile)
Function to set the specified Drawable as the tile for a particular integer key.

param
key
param
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;
    
public voidonDraw(android.graphics.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);
                }
            }
        }

    
protected voidonSizeChanged(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();
    
public voidresetTiles(int tilecount)
Rests the internal array of Bitmaps used for drawing tiles, and sets the maximum index of tiles to be inserted

param
tilecount

    	mTileArray = new Bitmap[tilecount];
    
public voidsetTile(int tileindex, int x, int 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

        mTileGrid[x][y] = tileindex;