简体   繁体   English

缩放Android可绘制资源

[英]Scaling Android drawable resource

I'm making a simple board game that uses a table view and a button array as the board squares. 我正在制作一个简单的棋盘游戏,它使用一个表格视图和一个按钮阵列作为棋盘方块。 My movePlayer takes a destination row and column, places a token in that row and column and replaces its previous position with a button. 我的movePlayer接受目标行和列,将令牌放置在该行和列中,并用按钮替换其先前位置。

My movePlayer method is: 我的movePlayer方法是:

private void movePlayer(int moveToRow, int moveToCol, Button buttons[][])
{
    int oldRow = player.getPlayerRow();
    int oldCol = player.getPlayerCol();
    Button oldPosition = buttons[oldRow][oldCol];
    int oldWidth = oldPosition.getWidth();
    int oldHeight = oldPosition.getHeight();
    Bitmap originalBitmap = BitmapFactory.decodeResource(getResources(),
            android.R.drawable.btn_default);
    Bitmap scaledBitmap = Bitmap.createScaledBitmap(originalBitmap, oldWidth, oldHeight, true);
    Resources resource = getResources();
    oldPosition.setBackground(new BitmapDrawable(resource, scaledBitmap));
    player.setPlayerRow(moveToRow);
    player.setPlayerCol(moveToCol);
    addPlayerToken(moveToRow, moveToCol, buttons);
}

I get the error java.lang.NullPointerException: Attempt to invoke virtual method 'int android.graphics.Bitmap.getWidth()' on a null object reference at android.graphics.Bitmap.createScaledBitmap(Bitmap.java:596) at foo.bar.minions.GameBoard.movePlayer(GameBoard.java:219) . 我收到error java.lang.NullPointerException: Attempt to invoke virtual method 'int android.graphics.Bitmap.getWidth()' on a null object reference at android.graphics.Bitmap.createScaledBitmap(Bitmap.java:596) at foo.bar.minions.GameBoard.movePlayer(GameBoard.java:219)

This code works if I replace android.R.drawable.btn_default with a drawable such as R.drawable.foo . 如果我将R.drawable.foo类的android.R.drawable.btn_default替换为可绘制对象,则此代码有效。

The code will also work if I replace everything below int oldWidth with oldPosition.setBackgroundResource(android.R.drawable.btn_default); 如果我用oldPosition.setBackgroundResource(android.R.drawable.btn_default);替换int oldWidth以下的所有内容,该代码也将起作用oldPosition.setBackgroundResource(android.R.drawable.btn_default); but the button isn't scaled properly or doesn't look like the other buttons. 但该按钮缩放比例不正确或与其他按钮看起来不一样。 For reference I use tableRow.addView(button); 作为参考,我使用tableRow.addView(button); to create my buttons. 创建我的按钮。

What can I do to make my buttons look the same? 我该怎么做才能使按钮看起来一样?

Fixed it, the problem was because the button that was drawn using tableRow.addView(button); 修复它,问题是因为使用tableRow.addView(button);绘制的tableRow.addView(button); is not the same image as android.R.drawable.btn_default so the fix was just adding button.setBackgroundResource(android.R.drawable.btn_default); android.R.drawable.btn_default图片不同,因此此修复仅添加了button.setBackgroundResource(android.R.drawable.btn_default); to before tableRow.addView(button); tableRow.addView(button);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM