In my android app, I have this function that creates a new bitmap from an old one via canvas.
在我的Android应用程序中,我有这个功能,通过画布从旧的位图创建一个新的位图。
private static Bitmap convert(Bitmap bitmap, Bitmap.Config config, int width, int height) {
Bitmap convertedBitmap = Bitmap.createBitmap(width, height, config);
Canvas canvas = new Canvas(convertedBitmap);
Paint paint = new Paint();
paint.setColor(Color.BLACK);
canvas.drawBitmap(bitmap, 0, 0, paint);
bitmap.recycle();
return convertedBitmap;
}
The problem is when I draw the old bitmap onto the canvas, since the old bitmap is bigger in dimensions than the canvas, only the top left part of the bitmap gets drawn on the canvas. Is there a way I can make it so when it draws the bitmap on the canvas, it scales the bitmap so it fits perfectly in the canvas?
问题是当我将旧位图绘制到画布上时,由于旧位图的尺寸大于画布,因此只有位图的左上部分会在画布上绘制。有没有一种方法可以让它在画布上绘制位图时,它会缩放位图,使其完全适合画布?
I don't want to resize the bitmap using createScaledBitmap
. Is there a faster way?
我不想使用createScaledBitmap调整位图的大小。有更快的方法吗?
OR Can I do createScaledBitmap
but make it mutable at the same time? That is what I am trying to do overall, resize and make mutable at same time.
或者我可以创建createScaledBitmap但同时使其可变吗?这就是我想要做的整体,调整大小并使其同时变得可变。
Thanks
谢谢
1 个解决方案
#1
8
You can call public void drawBitmap (Bitmap bitmap, Rect src, RectF dst, Paint paint)
for this function (Android Doc). The code below should accomplish what you are trying to do:
您可以为此函数调用public void drawBitmap(Bitmap位图,Rect src,RectF dst,Paint paint)(Android Doc)。下面的代码应该完成你想要做的事情:
canvas.drawBitmap(bitmap, null, new RectF(0, 0, canvasWidth, canvasHeight), null);
#1
8
You can call public void drawBitmap (Bitmap bitmap, Rect src, RectF dst, Paint paint)
for this function (Android Doc). The code below should accomplish what you are trying to do:
您可以为此函数调用public void drawBitmap(Bitmap位图,Rect src,RectF dst,Paint paint)(Android Doc)。下面的代码应该完成你想要做的事情:
canvas.drawBitmap(bitmap, null, new RectF(0, 0, canvasWidth, canvasHeight), null);