Reputation: 377
I'm new to Android and trying to create a simple game called SoS. It's close to complete however I can not figure out how to scale the images to put in the imagebuttons. The game board is created during runtime, the user chooses a board size, 6 x 6, 7 x 7, or 8 x 8 (ie: a board of 36, 49 or 64 tiles(imagebuttons)). Depending on the screen size/boardsize the images may or may not fit in the imagebuttons. So I decided to try and scale the images to the size of the ImageButton. I tried to scale the resource image to the correct size in the constructor of the Tile(imagebutton), and then store the result in a instance variable, however I get a runtime exception, and i'm not sure how to fix it. In the scaleImages method if I change the this.getLength(), and this.getWidth() to 50 it runs with out error but no images are set in the ImageButton. I think I need a way to determine the size of the button before I try and scale the image.
public class Tile extends ImageButton {
private boolean occupied;
private boolean isS;
private boolean isO;
int countClicks = 0;
private static Drawable sImageScaled;
private static Drawable oImageScaled;
private static Drawable emptyImageScaled;
public Tile(Context context) {
super(context);
scaleImages(context);
}
public Tile(Context context, AttributeSet attrs) {
super(context, attrs);
scaleImages(context);
}
public Tile(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
scaleImages(context);
}
public void setDefaults() {
occupied = false;
isS = false;
isO = false;
this.setBackground(emptyImageScaled);
}
// Method will set the background image of the Tile
// instance variable will keep track of which image is displayed and
// will change with every click
public void setTile() {
++countClicks;
switch (countClicks) {
case 1:
this.setBackground(sImageScaled);
isS = true;
isO = false;
break;
case 2:
this.setBackground(oImageScaled);
isO = true;
isS = false;
break;
case 3:
this.setBackground(emptyImageScaled);
isO = false;
isS = false;
}
// reset to 0
if (countClicks == 3) {
countClicks = countClicks % 3;
}
}
public void setOccupied(boolean val) {
occupied = val;
}
private void scaleImages(Context context){
// scale the s image
Drawable sDr = getResources().getDrawable(R.drawable.ic_simageorange);
Bitmap sBitmap = Bitmap.createBitmap(sDr.getIntrinsicWidth(), sDr.getIntrinsicHeight(), Config.ARGB_8888);
// Scale it
sImageScaled = new BitmapDrawable(context.getResources(), Bitmap.createScaledBitmap(sBitmap, this.getWidth(), this.getHeight(), true));
// scale the o image
Drawable oDr = getResources().getDrawable(R.drawable.ic_oimage);
Bitmap oBitmap = Bitmap.createBitmap(oDr.getIntrinsicWidth(), oDr.getIntrinsicHeight(), Config.ARGB_8888);
// Scale it
sImageScaled = new BitmapDrawable(context.getResources(), Bitmap.createScaledBitmap(oBitmap, this.getWidth(), this.getHeight(), true));
// scale the empty image
Drawable eDr = getResources().getDrawable(R.drawable.ic_tile);
Bitmap eBitmap = Bitmap.createBitmap(eDr.getIntrinsicWidth(), eDr.getIntrinsicHeight(), Config.ARGB_8888);
// Scale it to
emptyImageScaled = new BitmapDrawable(context.getResources(), Bitmap.createScaledBitmap(eBitmap, this.getWidth(), this.getHeight(), true));
}
}
Here's the code in the Activity that creates the views and the Tiles(ImageButtons)
// Method adds the views to the tableview
public void showGameBoard() {
int tilePadding = 1;
//galaxy S4
int tileWH = 100;
// Layout parameters for the TableRows
// (Layout params are always the parent class of the object receiving
// the param)
TableLayout.LayoutParams rowLp = new TableLayout.LayoutParams(
(tileWH * tilePadding) * boardSize.getWidth(), tileWH
* tilePadding, 1.0f);
// Layout parameters for the TableCells
TableRow.LayoutParams cellLp = new TableRow.LayoutParams(tileWH
* tilePadding, tileWH * tilePadding, 1.0f);
// for every row
for (int row = 0; row < tiles.length; row++) {
// create a new table row
TableRow tableRow = new TableRow(this);
// set the height and width of the row
tableRow.setLayoutParams(rowLp);
// for every column
for (int col = 0; col < tiles[row].length; col++) {
// add some padding to the tiles
tiles[row][col].setPadding(tilePadding, tilePadding,
tilePadding, tilePadding);
// add the tile to the table row
tableRow.addView(tiles[row][col], cellLp);
}
// add the row to the board layout
game_board.addView(tableRow);
}
}
// Method will set up the Tiles and the listeners
public void createGameBoard() {
// set total rows and columns based on the size of the board
int totalRows = boardSize.getWidth();
int totalCols = boardSize.getLength();
// setup the tiles array
tiles = new Tile[totalRows][totalCols];
// for every row
for (int row = 0; row < tiles.length; row++) {
// for every column
for (int col = 0; col < tiles[row].length; col++) {
// create a tile (ImageButton)
tiles[row][col] = new Tile(this);
// set the tile (ImageButton) defaults
tiles[row][col].setDefaults();
final int curRow = row;
final int curCol = col;
Well if you can solve my issue or point me in the right direction it'd be appreciated.
Upvotes: 0
Views: 951
Reputation: 648
I Don't know how your images for ImageButton
looks like, maybe 9-patch
will help you.
Here is simple guide about 9-patch
. To make 9-patch
image simply, change your image's file name from foo.png
into foo.9.png
, open it with android studio and draw fill area and scale area appropriately. if you don't use android studio, use sdk tools in /your_sdk_dir/toos/draw9patch
. because this is very simple tool, it needs no explanation.
Upvotes: 0