diff options
Diffstat (limited to 'samples/Snake/src/com/example/android/snake/TileView.java')
| -rwxr-xr-x[-rw-r--r--] | samples/Snake/src/com/example/android/snake/TileView.java | 128 |
1 files changed, 58 insertions, 70 deletions
diff --git a/samples/Snake/src/com/example/android/snake/TileView.java b/samples/Snake/src/com/example/android/snake/TileView.java index a912c53d6..a25e922ee 100644..100755 --- a/samples/Snake/src/com/example/android/snake/TileView.java +++ b/samples/Snake/src/com/example/android/snake/TileView.java @@ -25,18 +25,16 @@ 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. + * 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. + * 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; @@ -47,71 +45,54 @@ public class TileView extends View { private static int mXOffset; private static int mYOffset; + private final Paint mPaint = new Paint(); /** - * A hash that maps integer handles specified by the subclasser to the - * drawable that will be used for that reference + * A hash that maps integer handles specified by the subclasser to the drawable that will be + * used for that reference */ - private Bitmap[] mTileArray; + 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 + * 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); + public TileView(Context context, AttributeSet attrs) { + super(context, attrs); TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.TileView); + mTileSize = a.getDimensionPixelSize(R.styleable.TileView_tileSize, 12); - mTileSize = a.getInt(R.styleable.TileView_tileSize, 12); - a.recycle(); } - public TileView(Context context, AttributeSet attrs) { - super(context, attrs); + public TileView(Context context, AttributeSet attrs, int defStyle) { + super(context, attrs, defStyle); TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.TileView); + mTileSize = a.getDimensionPixelSize(R.styleable.TileView_tileSize, 12); - 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 + * Resets all tiles to 0 (empty) * - * @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(); + public void clearTiles() { + for (int x = 0; x < mXTileCount; x++) { + for (int y = 0; y < mYTileCount; y++) { + setTile(0, x, y); + } + } } /** - * Function to set the specified Drawable as the tile for a particular - * integer key. - * + * Function to set the specified Drawable as the tile for a particular integer key. + * * @param key * @param tile */ @@ -120,26 +101,38 @@ public class TileView extends View { 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); + @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); + } } } + + } + + /** + * 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]; } /** - * 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. + * 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 @@ -149,21 +142,16 @@ public class TileView extends View { 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); - } - } - } + 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(); } } |
