如何有效地调整位图的大小,而不会在android中丢失质量

我有一个大小为320x480Bitmap ,我需要在不同的设备屏幕上展开它,我尝试使用这个:

 Rect dstRect = new Rect(); canvas.getClipBounds(dstRect); canvas.drawBitmap(frameBuffer, null, dstRect, null); 

它工作,图像像我想要的那样填满了整个屏幕,但图像像素化,看起来很糟糕。 然后我试着:

 float scaleWidth = (float) newWidth / width; float scaleHeight = (float) newHeight / height; Matrix matrix = new Matrix(); matrix.postScale(scaleWidth, scaleHeight); Bitmap resizedBitmap = Bitmap.createBitmap(frameBuffer, 0, 0, width, height, matrix, true); canvas.drawBitmap(resizedBitmap, 0, 0, null); 

这一次看起来很完美,很好,很stream畅,但是这个代码必须在我的主循环中,并且每次迭代创buildBitmap都会使其非常缓慢。 我怎样才能调整我的形象,使它不像素化,并得到快速完成?

find解决scheme:

 Paint paint = new Paint(); paint.setFilterBitmap(); canvas.drawBitmap(bitmap, matrix, paint); 

调整位图大小:

 public Bitmap getResizedBitmap(Bitmap bm, int newHeight, int newWidth) { int width = bm.getWidth(); int height = bm.getHeight(); float scaleWidth = ((float) newWidth) / width; float scaleHeight = ((float) newHeight) / height; // create a matrix for the manipulation Matrix matrix = new Matrix(); // resize the bit map matrix.postScale(scaleWidth, scaleHeight); // recreate the new Bitmap Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, false); return resizedBitmap; } 

漂亮的自我解释:只需input原始位图对象和所需的位图尺寸,并且此方法将返回给您新resize的位图! 可能是,这对你有用。

我正在使用上述解决scheme来调整位图的大小。 但结果是图像的一部分丢失了。

这是我的代码。

  BitmapFactory.Options bmFactoryOptions = new BitmapFactory.Options(); bmFactoryOptions.inPreferredConfig = Bitmap.Config.ARGB_8888; bmFactoryOptions.inMutable = true; bmFactoryOptions.inSampleSize = 2; Bitmap originalCameraBitmap = BitmapFactory.decodeByteArray(pData, 0, pData.length, bmFactoryOptions); rotatedBitmap = getResizedBitmap(originalCameraBitmap, cameraPreviewLayout.getHeight(), cameraPreviewLayout.getWidth() - preSizePriviewHight(), (int) rotationDegrees); public Bitmap getResizedBitmap(Bitmap bm, int newWidth, int newHeight, int angle) { int width = bm.getWidth(); int height = bm.getHeight(); float scaleWidth = ((float) newWidth) / width; float scaleHeight = ((float) newHeight) / height; Matrix matrix = new Matrix(); matrix.postRotate(angle); matrix.postScale(scaleWidth, scaleHeight); Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, true); DeliverItApplication.getInstance().setImageCaptured(true); return resizedBitmap; } 

这里是图像的高度和宽度:预览表面大小:352:288resize之前的位图宽度:320高度:240 CameraPreviewLayout宽度:1080高度:1362resize的位图宽度:1022高度:1307