Modified Snake Sample app to run on non-touch devices like Google Tv and also devices with no dpad support

2D canvas DrawVertex to create a vector graphic background
touch & keypad playable
landscape & portrait for all known device sizes.  All measurements in DP and nicely scaling.
using xml drawables to rotate "dpad" arrow

Change-Id: I995acaaf0935c13c03fcbcf974ce5af06f46780e
This commit is contained in:
Kartic Grover
2012-05-01 11:02:58 -07:00
parent 424c95da4b
commit a1843415aa
21 changed files with 585 additions and 274 deletions

152
samples/Snake/src/com/example/android/snake/TileView.java Normal file → Executable file
View File

@@ -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,81 +45,37 @@ public class TileView extends View {
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);
/**
* A hash that maps integer handles specified by the subclasser to the drawable that will be
* used for that reference
*/
private Bitmap[] mTileArray;
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.TileView);
mTileSize = a.getInt(R.styleable.TileView_tileSize, 12);
a.recycle();
}
/**
* 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;
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();
}
/**
* 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];
}
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);
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
mXTileCount = (int) Math.floor(w / mTileSize);
mYTileCount = (int) Math.floor(h / mTileSize);
a.recycle();
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;
}
/**
@@ -137,9 +91,48 @@ public class TileView extends View {
}
/**
* 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.
* 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;
}
@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.
*
* @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();
}
}