如何在android中有效地调整位图大小且不损失质量 [英] How to resize a bitmap eficiently and with out losing quality in android

查看:36
本文介绍了如何在android中有效地调整位图大小且不损失质量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a Bitmap with a size of 320x480 and I need to stretch it on different device screens, I tried using this:

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

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

it works, the image fills the whole screen like I wanted but the image is pixelated and it looks bad. Then I tried :

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

this time it looks perfect, nice and smooth, but this code has to be in my main game loop and creating Bitmaps every iteration makes it very slow. How can I resize my image so it doesn't get pixelated and gets done fast?

找到解决方案:

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!可能吧,对你有用.

Pretty self explanatory: simply input the original Bitmap object and the desired dimensions of the Bitmap, and this method will return to you the newly resized Bitmap! May be, it is useful for you.

这篇关于如何在android中有效地调整位图大小且不损失质量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆