我已经搜索并尝试了几种方法来做到这一点,但无法做到这一点。以下代码产生了丑陋的结果;创建的位图包含来自多个图标的边,例如10%顶部和10%来自其他图标,80%来自目标图标。但第一个Bitmap(在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
答案 0 :(得分:1)
这是一个舍入误差,因为在原始精灵表上,每个单元格应为53x53。 但是,当你加载位图时,它会使它成为一个更大的位图,以适应设备的分辨率(hdpi)
通过从mdpi转换为hdpi,您的位图增长50%,单元格应为79.5像素宽。但是,当然,这个数字向下舍入到79像素,因此某些精灵的偏移误差。
如果你的精灵表是一个资源,那么你应该提供一个hdpi版本,它将是735x636像素。这样,你就可以自己控制转换(你会发现735不是10的倍数,所以也许你想稍微改变这些尺寸)。