我面临与Bitmap旋转相关的问题,问题是跟随代码旋转位图很好但是在画布上绘制旋转位图时有背景背景,我看到这只适用于上面的5.0版本和4.0我得到透明背景...任何线索请分享。
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);
答案 0 :(得分:2)
我也遇到了这个问题。在Goolge之后,我发现如果您使用BitmapFactory.decodeResource,则无法在某些设备上修复此问题。所以我使用这些代码而不是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的旋转位图。