0

我是 Android 新手,正在尝试创建一个名为 SoS 的简单游戏。它接近完成但是我不知道如何缩放图像以放入图像按钮。游戏板在运行时创建,用户选择板尺寸,6 x 6、7 x 7 或 8 x 8(即:36、49 或 64 块(图像按钮)的板)。根据屏幕尺寸/电路板尺寸,图像可能适合也可能不适合图像按钮。所以我决定尝试将图像缩放到 ImageButton 的大小。我试图在 Tile(imagebutton) 的构造函数中将资源图像缩放到正确的大小,然后将结果存储在实例变量中,但是我得到一个运行时异常,我不知道如何修复它。在 scaleImages 方法中,如果我更改 this.getLength() 和 this。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));

}

}

这是 Activity 中创建视图和 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;

好吧,如果您能解决我的问题或为我指明正确的方向,我们将不胜感激。

4

1 回答 1

0

我不知道您的图像ImageButton看起来如何,也许9-patch会对您有所帮助。 是关于9-patch. 为了9-patch简单地制作图像,将图像的文件名从 更改foo.pngfoo.9.png,用android studio打开它并适当地绘制填充区域和缩放区域。如果您不使用 android studio,请使用/your_sdk_dir/toos/draw9patch. 因为这是一个非常简单的工具,所以不需要解释。

于 2014-08-18T01:55:06.657 回答