1 /*
2  * Copyright (C) 2007 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.example.android.snake;
18 
19 import android.content.Context;
20 import android.content.res.TypedArray;
21 import android.graphics.Bitmap;
22 import android.graphics.Canvas;
23 import android.graphics.Paint;
24 import android.graphics.drawable.Drawable;
25 import android.util.AttributeSet;
26 import android.view.View;
27 
28 /**
29  * TileView: a View-variant designed for handling arrays of "icons" or other drawables.
30  *
31  */
32 public class TileView extends View {
33 
34     /**
35      * Parameters controlling the size of the tiles and their range within view. Width/Height are in
36      * pixels, and Drawables will be scaled to fit to these dimensions. X/Y Tile Counts are the
37      * number of tiles that will be drawn.
38      */
39 
40     protected static int mTileSize;
41 
42     protected static int mXTileCount;
43     protected static int mYTileCount;
44 
45     private static int mXOffset;
46     private static int mYOffset;
47 
48     private final Paint mPaint = new Paint();
49 
50     /**
51      * A hash that maps integer handles specified by the subclasser to the drawable that will be
52      * used for that reference
53      */
54     private Bitmap[] mTileArray;
55 
56     /**
57      * A two-dimensional array of integers in which the number represents the index of the tile that
58      * should be drawn at that locations
59      */
60     private int[][] mTileGrid;
61 
TileView(Context context, AttributeSet attrs)62     public TileView(Context context, AttributeSet attrs) {
63         super(context, attrs);
64 
65         TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.TileView);
66         mTileSize = a.getDimensionPixelSize(R.styleable.TileView_tileSize, 12);
67 
68         a.recycle();
69     }
70 
TileView(Context context, AttributeSet attrs, int defStyle)71     public TileView(Context context, AttributeSet attrs, int defStyle) {
72         super(context, attrs, defStyle);
73 
74         TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.TileView);
75         mTileSize = a.getDimensionPixelSize(R.styleable.TileView_tileSize, 12);
76 
77         a.recycle();
78 
79     }
80 
81     /**
82      * Resets all tiles to 0 (empty)
83      *
84      */
clearTiles()85     public void clearTiles() {
86         for (int x = 0; x < mXTileCount; x++) {
87             for (int y = 0; y < mYTileCount; y++) {
88                 setTile(0, x, y);
89             }
90         }
91     }
92 
93     /**
94      * Function to set the specified Drawable as the tile for a particular integer key.
95      *
96      * @param key
97      * @param tile
98      */
loadTile(int key, Drawable tile)99     public void loadTile(int key, Drawable tile) {
100         Bitmap bitmap = Bitmap.createBitmap(mTileSize, mTileSize, Bitmap.Config.ARGB_8888);
101         Canvas canvas = new Canvas(bitmap);
102         tile.setBounds(0, 0, mTileSize, mTileSize);
103         tile.draw(canvas);
104 
105         mTileArray[key] = bitmap;
106     }
107 
108     @Override
onDraw(Canvas canvas)109     public void onDraw(Canvas canvas) {
110         super.onDraw(canvas);
111         for (int x = 0; x < mXTileCount; x += 1) {
112             for (int y = 0; y < mYTileCount; y += 1) {
113                 if (mTileGrid[x][y] > 0) {
114                     canvas.drawBitmap(mTileArray[mTileGrid[x][y]], mXOffset + x * mTileSize,
115                             mYOffset + y * mTileSize, mPaint);
116                 }
117             }
118         }
119 
120     }
121 
122     /**
123      * Rests the internal array of Bitmaps used for drawing tiles, and sets the maximum index of
124      * tiles to be inserted
125      *
126      * @param tilecount
127      */
128 
resetTiles(int tilecount)129     public void resetTiles(int tilecount) {
130         mTileArray = new Bitmap[tilecount];
131     }
132 
133     /**
134      * Used to indicate that a particular tile (set with loadTile and referenced by an integer)
135      * should be drawn at the given x/y coordinates during the next invalidate/draw cycle.
136      *
137      * @param tileindex
138      * @param x
139      * @param y
140      */
setTile(int tileindex, int x, int y)141     public void setTile(int tileindex, int x, int y) {
142         mTileGrid[x][y] = tileindex;
143     }
144 
145     @Override
onSizeChanged(int w, int h, int oldw, int oldh)146     protected void onSizeChanged(int w, int h, int oldw, int oldh) {
147         mXTileCount = (int) Math.floor(w / mTileSize);
148         mYTileCount = (int) Math.floor(h / mTileSize);
149 
150         mXOffset = ((w - (mTileSize * mXTileCount)) / 2);
151         mYOffset = ((h - (mTileSize * mYTileCount)) / 2);
152 
153         mTileGrid = new int[mXTileCount][mYTileCount];
154         clearTiles();
155     }
156 
157 }
158