Android - Resizing Bitmap Cuts It Instead Of Scaling It
I have an application where the user can draw to a view whose dimensions are W = Match Parent and H = 250dp. I need to resize it to W = 399pixels and H = 266pixels so I can print i
Solution 1:
Try to use Matrix
to resize bitmap.
publicstatic Bitmap resizeBitmap(Bitmap bitmap, int width, int height) {
intw= bitmap.getWidth();
inth= bitmap.getHeight();
Matrixmatrix=newMatrix();
floatscaleWidth= ((float) width / w);
floatscaleHeight= ((float) height / h);
matrix.postScale(scaleWidth, scaleHeight);
Bitmapnewbmp= Bitmap.createBitmap(bitmap, 0, 0, w, h, matrix, true);
return newbmp;
}
Solution 2:
publicstatic Bitmap resizeBitmap(Bitmap bitmap, int width, int height) { //width - height in pixel not in DP
bitmap.setDensity(Bitmap.DENSITY_NONE);
Bitmapnewbmp= Bitmap.createScaledBitmap(bitmap, width, height, true);
return newbmp;
}
Solution 3:
The problem is that the Bitmap of the resized image is instantiated right after the creation of the bitmap that gets the user input. This was my code that worked.
However, note that I made an imageView to hold the resized image.
ByteArrayOutputStreamstream=newByteArrayOutputStream();
//get the user input and store in a bitmapBitmapbitmap= Bitmap.createBitmap(mView.getWidth(), mView.getHeight(), Bitmap.Config.ARGB_8888);
//Create a canvas containing white background and draw the user input on it//this is necessary because the png format thinks that white is transparentCanvasc=newCanvas(bitmap);
c.drawColor(Color.WHITE);
c.drawBitmap(bitmap, 0, 0, null);
//redraw
mView.draw(c);
//create resized image and displayBitmapresizedImage= Bitmap.createScaledBitmap(bitmap, 399, 266, true);
imageView.setImageBitmap(resizedImage);
Post a Comment for "Android - Resizing Bitmap Cuts It Instead Of Scaling It"