私は Android が初めてで、SoS という単純なゲームを作成しようとしています。完成に近づいていますが、画像を拡大縮小して画像ボタンに入れる方法がわかりません。ゲーム ボードは実行時に作成され、ユーザーはボード サイズを 6 x 6、7 x 7、または 8 x 8 (つまり、36、49、または 64 個のタイル (画像ボタン) のボード) から選択します。画面サイズ/ボードサイズによっては、画像が画像ボタンに収まる場合と収まらない場合があります。そこで、画像を ImageButton のサイズに合わせて拡大縮小することにしました。Tile(imagebutton) のコンストラクターでリソース イメージを正しいサイズにスケーリングし、その結果をインスタンス変数に格納しようとしましたが、ランタイム例外が発生し、修正方法がわかりません。this.getLength() を変更した場合、scaleImages メソッドで、これを変更します。getWidth() を 50 にすると、エラーなしで実行されますが、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) を作成する Activity のコードは次のとおりです。
// 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;
私の問題を解決するか、正しい方向に向けていただければ幸いです。