How to resize bitmap when drawing in canvas?

半腔热情 提交于 2019-12-07 12:25:43

问题


In my android app, I have this function that creates a new bitmap from an old one via canvas.

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?

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.

Thanks


回答1:


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:

canvas.drawBitmap(bitmap, null, new RectF(0, 0, canvasWidth, canvasHeight), null);


来源:https://stackoverflow.com/questions/27466099/how-to-resize-bitmap-when-drawing-in-canvas

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!