如何在 Android 中有效地调整位图大小且不损失质量

问题描述 投票:0回答:1

我有一个尺寸为

Bitmap
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
都会使其非常慢。如何调整图像大小,使其不会像素化并快速完成?

找到解决方案:

Paint paint = new Paint();
paint.setFilterBitmap();
canvas.drawBitmap(bitmap, matrix, paint);
android canvas bitmap resize surfaceview
1个回答
0
投票

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

这是我的代码。

 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 CameraPreviewLayout 宽度:1080 高度:1362 调整大小的位图宽度:1022 高度:1307

© www.soinside.com 2019 - 2024. All rights reserved.