我是Android新手并尝试创建一款名为SoS的简单游戏。它已接近完成,但我无法弄清楚如何缩放图像以放入图像按钮。游戏板在运行时创建,用户选择6 x 6,7 x 7或8 x 8的板尺寸(即:36,49或64块(图像按钮)板)。根据屏幕尺寸/ boardize,图像可能适合或不适合图像按钮。所以我决定尝试将图像缩放到ImageButton的大小。我试图在Tile(imagebutton)的构造函数中将资源图像缩放到正确的大小,然后将结果存储在实例变量中,但是我得到运行时异常,我不知道如何修复它。在scaleImages方法中,如果我将this.getLength()和this.getWidth()更改为50,则会运行out out error但在ImageButton中没有设置图像。在尝试缩放图像之前,我想我需要一种方法来确定按钮的大小。
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));
}
}
这里是创建视图的活动中的代码和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;
如果你能解决我的问题或指出我正确的方向,那么我们将不胜感激。
答案 0 :(得分:0)
我不知道ImageButton
的图像是什么样的,也许9-patch
可以帮到你。
Here是关于9-patch
的简单指南。要简单地制作9-patch
图片,请将图片的文件名从foo.png
更改为foo.9.png
,然后使用android studio打开并适当地绘制填充区域和缩放区域。如果你不使用android studio,请在/your_sdk_dir/toos/draw9patch
中使用sdk工具。因为这是一个非常简单的工具,所以不需要解释。