Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to resize bitmap when drawing in canvas?

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

like image 899
omega Avatar asked Dec 14 '14 03:12

omega


People also ask

Can you resize a bitmap?

Resizing a BMP image is the only practical way to reduce the file size without changing the format. The tool to perform this action is already included in the Windows operating system.

How do I create a bitmap in canvas?

To create a bitmap object on a canvas C , use: id = C . create_bitmap( x , y , *options ...) which returns the integer ID number of the image object for that canvas.

What is bitmap in canvas?

Canvas is the place or medium where perfroms/executes the operation of drawing, and Bitmap is responsible for storing the pixel of the picture you draw.


1 Answers

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);
like image 60
Kai Avatar answered Oct 21 '22 17:10

Kai