createBitmap无法正确加载子图像或绘制(Canvas)无法正确显示

时间:2014-12-30 18:04:06

标签: android bitmap

我有一个资源," cards.png"其中包含一副纸牌所需的所有卡片。

资源正好是896x352,每张卡正好是64x88。

"卡"对象是一个简单的对象,包含套装的字符串,值的字符串和位图。

我使用" Bitmap.createBitmap"从大原始图像创建子图像。 后来,当我绘制时,它似乎没有显示该函数正确加载图像。有些卡是"偏移"通过多个像素,当我尝试从左到右读取图像时,它似乎变得更糟。

我已经尝试了所有的东西,发现这里有很多未回答的类似问题,所以想知道我是否会以错误的方式解决这个问题?

资源" R.drawable.cards"在" / game / res / drawable"中引用文件夹中。

加载卡位图的代码是:

public GameScreen(Context context){
    int x=0, y=0;
    Bitmap cards = BitmapFactory.decodeResource(context.getResources(), R.drawable.cards);
    for (Card card: deck.getCards()){
        if (card.getSuit().equals("C")){
            y = 0;
        } else if (card.getSuit().equals("D")){
            y = 88;
        } else if (card.getSuit().equals("H")){
            y = 176;
        } else if (card.getSuit().equals("S")){
            y = 264;
        } else {
            x=0;
            y=0;
        }

        if (card.getValue().equals("A")){
            x = 0;
        } else if (card.getValue().equals("10")){
            x = 576;
        } else if (card.getValue().equals("J")){
            x = 640;
        } else if (card.getValue().equals("Q")){
            x = 704;
        } else if (card.getValue().equals("K")){
            x = 768;
        } else if (card.getValue().equals("Joker")){
            // Joker
            x = 832;
        } else {
            x = 64 * (Integer.parseInt(card.getValue())-1);
        }

        Bitmap bitmap = Bitmap.createBitmap(cards, x, y, CardBitmapWidth, CardBitmapHeight);
        card.setBitmap(bitmap);

    }
}

要绘制的代码是:

public void draw(Canvas canvas){
    int x = 0;
    int y = 0;

    for (Card card: cards){
        canvas.drawBitmap(card.getBitmap(), x, y, null);
        x += card.getBitmap().getWidth();
        if (x>canvas.getWidth()){
            x = 0;
            y += card.getBitmap.getHeight();
        }
     }
}

1 个答案:

答案 0 :(得分:0)

确保加载时图像没有缩放:

BitmapFactory.Options options = new BitmapFactory.Options();
options.inScaled = false;
Bitmap cards = BitmapFactory.decodeResource(context.getResources(), R.drawable.cards, options);
相关问题