Tuesday, 15 January 2013

android - How to read sprite position properly on the loop; im able see edges of the other images :\ -



android - How to read sprite position properly on the loop; im able see edges of the other images :\ -

i've searched , tried few ways this, can't right. next code produces ugly result; created bitmaps contains edges multiple icons, 10% top , 10% left other icons , 80% target icon. first bitmap (the 1 created on x=0, y=0) perfect rest not.

please allow me know im doing wrong here. cheers!

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{ (int i=0; i<rows; += 1) { (int j=0; j<cols; j += 1) { /* //same result 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()", "(" + + "," + j + ") x: " + j*width + ", y: " + (i*height) + ", w: " + width + ", h: " + height); } } }catch(exception e){} homecoming iconbox_tmp; }

edit:

here's logcat output:

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

that's rounding error, because on original sprite sheet, each cell should 53x53. but, when load bitmap, makes bigger bitmap fit resolution of device (hdpi)

by converting mdpi hdpi, bitmap grows 50%, , cells should 79.5 pixels wide. of course, number rounded downwards 79 pixel, offset error sprites.

if sprite sheet resource, should provide hdpi version, 735x636 pixels. way, you'll able command conversion (and you'll find 735 not multiple of 10, maybe you'll want alter these dimensions slightly).

android sprite sprite-sheet

No comments:

Post a Comment