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

如何在android中有效地调整位图的大小而不丢失质量,android,canvas,bitmap,resize,surfaceview,Android,Canvas,Bitmap,Resize,Surfaceview,我有一个位图,大小为320x480,我需要在不同的设备屏幕上拉伸它,我尝试使用以下方法: Rect dstRect = new Rect(); canvas.getClipBounds(dstRect); canvas.drawBitmap(frameBuffer, null, dstRect, null); 它可以工作,图像像我想要的那样充满整个屏幕,但是图像是像素化的,看起来很糟糕。然后我试着: float scaleWidth = (float) newWidth / width; fl

我有一个
位图
,大小为
320x480
,我需要在不同的设备屏幕上拉伸它,我尝试使用以下方法:

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);
这一次它看起来完美、漂亮、流畅,但这段代码必须在我的主游戏循环中,并且每次迭代创建
Bitmap
s都会让它变得非常慢。如何调整图像大小,使其不会像素化并快速完成

找到了解决方案:

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;
}
非常简单:只需输入原始位图对象和所需的位图尺寸,此方法将返回新调整大小的位图!
可能是,它对您很有用。

我正在使用上述解决方案调整位图的大小。但这会导致图像的一部分丢失

这是我的密码

 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:288 调整大小前位图宽度:320高度:240 摄影机预览布局宽度:1080高度:1362
调整大小的位图宽度:1022高度:1307

如果您找到了自己的解决方案,您应该将其添加为问题的答案,并自己接受,这样其他人就不会向您的问题添加不需要的答案。@user924941此一次性调整大小比调整每一帧的大小要好。要获得更高质量的缩放图像,位图resizedBitmap=bitmap.createBitmap(bm,0,0,width,height,matrix,true);@BorisKarloff:设置过滤器参数在放大时没有任何效果,OP对此感兴趣。如何调用该函数?为什么不使用Bitmap resizedBitmap=Bitmap.createScaledBitmap(originalBitmap,newWidth,newHeight,false);