0

我已经搜索并尝试了几种方法来做到这一点,但无法做到这一点。以下代码会产生丑陋的结果;创建的位图包含来自多个图标的边缘,例如 10% 的顶部和 10% 的左侧来自其他图标以及 80% 来自目标图标。但是第一个位图(在 x=0,y=0 上创建的位图)是完美的,但其余的则不是。

请让我知道我在这里做错了什么。干杯!

private Bitmap[][]  createIcons(Bitmap sprite_sheet, int rows, int cols)
{
    Bitmap[][] iconBox_TMP = new Bitmap[20][20];
    int width   = sprite_sheet.getWidth()   / cols;
    int height  = sprite_sheet.getHeight()  / rows;

    try{


        for (int i=0; i<rows; i += 1) 
        {

            for (int j=0; j<cols; j += 1) 
            {

                /*
                            //same result with matrix
                Matrix matrix = new Matrix();
                matrix.postScale(1.0f, 1.0f);
                    iconBox_TMP[i][j] = Bitmap.createBitmap(
                            sprite_sheet, 
                            j*width, 
                            i*height, 
                            width, 
                            height, 
                            matrix, 
                            true);
                    matrix.reset();
                    matrix = null;
                    */

                    iconBox_TMP[i][j] = Bitmap.createBitmap(sprite_sheet,
                    width * j, 
                    height * i, 
                    width, 
                    height);

                    Log.d("createIcons()", "(" + i + "," + j + ") X: " + j*width + ", Y: " + (i*height) + ", W: " + width + ", H: " + height);

            }

        }   


    }catch(Exception e){}

    return iconBox_TMP;
}

编辑:

这是 LogCat 输出:

02-10 04:41:01.281: D/createIcons()(4961): (0,0) X: 0, Y: 0, W: 79, H: 79
02-10 04:41:01.281: D/createIcons()(4961): (0,1) X: 79, Y: 0, W: 79, H: 79
02-10 04:41:01.281: D/createIcons()(4961): (0,2) X: 158, Y: 0, W: 79, H: 79
02-10 04:41:01.281: D/createIcons()(4961): (0,3) X: 237, Y: 0, W: 79, H: 79
02-10 04:41:01.281: D/createIcons()(4961): (0,4) X: 316, Y: 0, W: 79, H: 79
02-10 04:41:01.285: D/createIcons()(4961): (0,5) X: 395, Y: 0, W: 79, H: 79
02-10 04:41:01.285: D/createIcons()(4961): (0,6) X: 474, Y: 0, W: 79, H: 79
02-10 04:41:01.285: D/createIcons()(4961): (0,7) X: 553, Y: 0, W: 79, H: 79
02-10 04:41:01.285: D/createIcons()(4961): (0,8) X: 632, Y: 0, W: 79, H: 79
02-10 04:41:01.285: D/createIcons()(4961): (0,9) X: 711, Y: 0, W: 79, H: 79
02-10 04:41:01.285: D/createIcons()(4961): (1,0) X: 0, Y: 79, W: 79, H: 79
02-10 04:41:01.285: D/createIcons()(4961): (1,1) X: 79, Y: 79, W: 79, H: 79
02-10 04:41:01.289: D/createIcons()(4961): (1,2) X: 158, Y: 79, W: 79, H: 79
02-10 04:41:01.289: D/createIcons()(4961): (1,3) X: 237, Y: 79, W: 79, H: 79
02-10 04:41:01.289: D/createIcons()(4961): (1,4) X: 316, Y: 79, W: 79, H: 79
02-10 04:41:01.289: D/createIcons()(4961): (1,5) X: 395, Y: 79, W: 79, H: 79
02-10 04:41:01.289: D/createIcons()(4961): (1,6) X: 474, Y: 79, W: 79, H: 79
02-10 04:41:01.289: D/createIcons()(4961): (1,7) X: 553, Y: 79, W: 79, H: 79
02-10 04:41:01.289: D/createIcons()(4961): (1,8) X: 632, Y: 79, W: 79, H: 79
02-10 04:41:01.289: D/createIcons()(4961): (1,9) X: 711, Y: 79, W: 79, H: 79
02-10 04:41:01.292: D/createIcons()(4961): (2,0) X: 0, Y: 158, W: 79, H: 79
02-10 04:41:01.292: D/createIcons()(4961): (2,1) X: 79, Y: 158, W: 79, H: 79
02-10 04:41:01.292: D/createIcons()(4961): (2,2) X: 158, Y: 158, W: 79, H: 79
02-10 04:41:01.292: D/createIcons()(4961): (2,3) X: 237, Y: 158, W: 79, H: 79
02-10 04:41:01.292: D/createIcons()(4961): (2,4) X: 316, Y: 158, W: 79, H: 79
02-10 04:41:01.292: D/createIcons()(4961): (2,5) X: 395, Y: 158, W: 79, H: 79
02-10 04:41:01.292: D/createIcons()(4961): (2,6) X: 474, Y: 158, W: 79, H: 79
02-10 04:41:01.292: D/createIcons()(4961): (2,7) X: 553, Y: 158, W: 79, H: 79
02-10 04:41:01.292: D/createIcons()(4961): (2,8) X: 632, Y: 158, W: 79, H: 79
02-10 04:41:01.296: D/createIcons()(4961): (2,9) X: 711, Y: 158, W: 79, H: 79
02-10 04:41:01.296: D/createIcons()(4961): (3,0) X: 0, Y: 237, W: 79, H: 79
02-10 04:41:01.296: D/createIcons()(4961): (3,1) X: 79, Y: 237, W: 79, H: 79
02-10 04:41:01.296: D/createIcons()(4961): (3,2) X: 158, Y: 237, W: 79, H: 79
02-10 04:41:01.296: D/createIcons()(4961): (3,3) X: 237, Y: 237, W: 79, H: 79
02-10 04:41:01.296: D/createIcons()(4961): (3,4) X: 316, Y: 237, W: 79, H: 79
02-10 04:41:01.296: D/createIcons()(4961): (3,5) X: 395, Y: 237, W: 79, H: 79
02-10 04:41:01.296: D/createIcons()(4961): (3,6) X: 474, Y: 237, W: 79, H: 79
02-10 04:41:01.300: D/createIcons()(4961): (3,7) X: 553, Y: 237, W: 79, H: 79
02-10 04:41:01.324: D/createIcons()(4961): (3,8) X: 632, Y: 237, W: 79, H: 79
02-10 04:41:01.324: D/createIcons()(4961): (3,9) X: 711, Y: 237, W: 79, H: 79
02-10 04:41:01.324: D/createIcons()(4961): (4,0) X: 0, Y: 316, W: 79, H: 79
02-10 04:41:01.324: D/createIcons()(4961): (4,1) X: 79, Y: 316, W: 79, H: 79
02-10 04:41:01.328: D/createIcons()(4961): (4,2) X: 158, Y: 316, W: 79, H: 79
02-10 04:41:01.328: D/createIcons()(4961): (4,3) X: 237, Y: 316, W: 79, H: 79
02-10 04:41:01.328: D/createIcons()(4961): (4,4) X: 316, Y: 316, W: 79, H: 79
02-10 04:41:01.328: D/createIcons()(4961): (4,5) X: 395, Y: 316, W: 79, H: 79
02-10 04:41:01.328: D/createIcons()(4961): (4,6) X: 474, Y: 316, W: 79, H: 79
02-10 04:41:01.328: D/createIcons()(4961): (4,7) X: 553, Y: 316, W: 79, H: 79
02-10 04:41:01.328: D/createIcons()(4961): (4,8) X: 632, Y: 316, W: 79, H: 79
02-10 04:41:01.328: D/createIcons()(4961): (4,9) X: 711, Y: 316, W: 79, H: 79
02-10 04:41:01.332: D/createIcons()(4961): (5,0) X: 0, Y: 395, W: 79, H: 79
02-10 04:41:01.332: D/createIcons()(4961): (5,1) X: 79, Y: 395, W: 79, H: 79
02-10 04:41:01.332: D/createIcons()(4961): (5,2) X: 158, Y: 395, W: 79, H: 79
02-10 04:41:01.332: D/createIcons()(4961): (5,3) X: 237, Y: 395, W: 79, H: 79
02-10 04:41:01.332: D/createIcons()(4961): (5,4) X: 316, Y: 395, W: 79, H: 79
02-10 04:41:01.332: D/createIcons()(4961): (5,5) X: 395, Y: 395, W: 79, H: 79
02-10 04:41:01.332: D/createIcons()(4961): (5,6) X: 474, Y: 395, W: 79, H: 79
02-10 04:41:01.332: D/createIcons()(4961): (5,7) X: 553, Y: 395, W: 79, H: 79
02-10 04:41:01.335: D/createIcons()(4961): (5,8) X: 632, Y: 395, W: 79, H: 79
02-10 04:41:01.335: D/createIcons()(4961): (5,9) X: 711, Y: 395, W: 79, H: 79
02-10 04:41:01.335: D/createIcons()(4961): (6,0) X: 0, Y: 474, W: 79, H: 79
02-10 04:41:01.335: D/createIcons()(4961): (6,1) X: 79, Y: 474, W: 79, H: 79
02-10 04:41:01.335: D/createIcons()(4961): (6,2) X: 158, Y: 474, W: 79, H: 79
02-10 04:41:01.335: D/createIcons()(4961): (6,3) X: 237, Y: 474, W: 79, H: 79
02-10 04:41:01.335: D/createIcons()(4961): (6,4) X: 316, Y: 474, W: 79, H: 79
02-10 04:41:01.335: D/createIcons()(4961): (6,5) X: 395, Y: 474, W: 79, H: 79
02-10 04:41:01.339: D/createIcons()(4961): (6,6) X: 474, Y: 474, W: 79, H: 79
02-10 04:41:01.339: D/createIcons()(4961): (6,7) X: 553, Y: 474, W: 79, H: 79
02-10 04:41:01.339: D/createIcons()(4961): (6,8) X: 632, Y: 474, W: 79, H: 79
02-10 04:41:01.339: D/createIcons()(4961): (6,9) X: 711, Y: 474, W: 79, H: 79
02-10 04:41:01.339: D/createIcons()(4961): (7,0) X: 0, Y: 553, W: 79, H: 79
02-10 04:41:01.339: D/createIcons()(4961): (7,1) X: 79, Y: 553, W: 79, H: 79
02-10 04:41:01.339: D/createIcons()(4961): (7,2) X: 158, Y: 553, W: 79, H: 79
02-10 04:41:01.339: D/createIcons()(4961): (7,3) X: 237, Y: 553, W: 79, H: 79
02-10 04:41:01.343: D/createIcons()(4961): (7,4) X: 316, Y: 553, W: 79, H: 79
02-10 04:41:01.343: D/createIcons()(4961): (7,5) X: 395, Y: 553, W: 79, H: 79
02-10 04:41:01.343: D/createIcons()(4961): (7,6) X: 474, Y: 553, W: 79, H: 79
02-10 04:41:01.343: D/createIcons()(4961): (7,7) X: 553, Y: 553, W: 79, H: 79
02-10 04:41:01.343: D/createIcons()(4961): (7,8) X: 632, Y: 553, W: 79, H: 79
02-10 04:41:01.343: D/createIcons()(4961): (7,9) X: 711, Y: 553, W: 79, H: 79
4

1 回答 1

1

这是一个舍入错误,因为在您的原始精灵表上,每个单元格应该是 53x53。但是,当您加载位图时,它会使其成为更大的位图以适应设备的分辨率 (hdpi)

通过从 mdpi 转换为 hdpi,您的位图将增长 50%,并且单元格应为 79.5 像素宽。但当然,这个数字向下舍入到 79 像素,因此某些精灵的偏移误差。

如果您的精灵表是一个资源,那么您应该提供一个 hdpi 版本,它将是 735x636 像素。这样,您将能够自己控制转换(您会发现 735 不是 10 的倍数,因此您可能需要稍微更改这些尺寸)。

于 2013-02-10T20:37:47.673 回答