Android: bitmap rotation leads to black background -
i'm facing issue related bitmap rotation, issue follow code rotates bitmap fine background when draw rotated bitmap on canvas, see version 5.0 above , 4.0 transparent background... clue please share.
int resid = context.getresources().getidentifier(mdrawablename, "drawable", context.getpackagename()); matrix mat = new matrix(); mat.postrotate(i*6%30); // angle rotated bitmap logobm = bitmapfactory.decoderesource(context.getresources(), resid) ; logobm = density.getinstance().scaleit(logobm, density.getinstance().getpixelfordp(80), 0); logobm = bitmap.createbitmap(logobm, 0, 0, logobm.getwidth(), logobm.getheight(), mat, true);
i issue. after goolge, find if use bitmapfactory.decoderesource,this issue can't fixed @ devices. use these code instead of bitmapfactory.decoderesource:
bitmap bitmap = yourbitmap; matrix matrix = new matrix(); matrix.postrotate(angle); rect srcr = new rect(0, 0, bitmap.getwidth(), bitmap.getheight()); rectf dstr = new rectf(0, 0, bitmap.getwidth(), bitmap.getheight()); rectf devicer = new rectf(); matrix.maprect(devicer, dstr); int neww = math.round(devicer.width()); int newh = math.round(devicer.height()); bitmap result = bitmap.createbitmap(neww, newh, bitmap.config.argb_8888); result.erasecolor(color.transparent); canvas canvas = new canvas(); canvas.translate(-devicer.left, -devicer.top); canvas.concat(matrix); paint paint = new paint(); paint.setantialias(true); paint.setfilterbitmap(true); canvas.setbitmap(result); canvas.drawbitmap(bitmap, srcr, dstr, paint); canvas.setbitmap(null);
the 'result' bitmap rotated bitmap transparent bg.
Comments
Post a Comment