gpt4 book ai didi

android - 如何在 android 中有效且不损失质量的情况下调整位图的大小

转载 作者:可可西里 更新时间:2023-11-01 18:52:08 26 4
gpt4 key购买 nike

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

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);

最佳答案

调整位图大小:

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;
}

不言自明:只需输入原始 Bitmap 对象和所需的 Bitmap 尺寸,此方法就会返回给您新调整大小的 Bitmap!可能对你有用。

关于android - 如何在 android 中有效且不损失质量的情况下调整位图的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8327846/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com