Android:位图旋转导致黑色背景 [英] Android: bitmap rotation leads to black background

查看:139
本文介绍了Android:位图旋转导致黑色背景的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到一个与位图旋转相关的问题,问题是跟随代码可以很好地旋转位图,但是在画布上绘制旋转位图时具有背景,我只在5.0以上版本和4.0上看到此背景透明背景.任何线索请分享.

I'm facing an issue related to Bitmap rotation, issue is follow code rotates the bitmap fine but with a back background when draw rotated bitmap on canvas, I see this only for version 5.0 above and 4.0 I get transparent background... any clue please share.

            int resID = context.getResources().getIdentifier(mDrawableName,
                    "drawable", context.getPackageName());
            Matrix mat = new Matrix();
            mat.postRotate(i*6%30); // angle to be 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);

推荐答案

我也遇到了这个问题.在Goolge之后,我发现如果您使用BitmapFactory.decodeResource,则此问题无法在某些设备上解决.所以我用这些代码代替BitmapFactory.decodeResource:

I also get this issue. After Goolge, I find that if you use BitmapFactory.decodeResource,this issue can't be fixed at some devices. So I 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);

结果"位图是具有透明BG的旋转位图.

the 'result' Bitmap is your rotated bitmap with transparent BG.

这篇关于Android:位图旋转导致黑色背景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆