如何eficiently并在安卓不失品位调整位图 [英] How to resize a bitmap eficiently and with out losing quality in android

查看:88
本文介绍了如何eficiently并在安卓不失品位调整位图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个位图与大小小320x480 ,我需要舒展它在不同的设备屏幕,我尝试使用这样的:

 矩形dstRect =新的矩形();
canvas.getClipBounds(dstRect);
canvas.drawBitmap(FRAMEBUFFER,空,dstRect,NULL);
 

它的工作原理,图像填充整个屏幕像我想,但图像是像素化,它的日子也不好过。然后我尝试:

 浮动scaleWidth =(浮点)newWidth /宽;
浮动scaleHeight =(浮点)newHeight /高度;
字模=新的Matrix();
matrix.postScale(scaleWidth,scaleHeight);
位图resizedBitmap = Bitmap.createBitmap(帧缓冲,0,0,
                宽度,高度,矩阵,真);
canvas.drawBitmap(resizedBitmap,0,0,NULL);
 

这个时候它看起来很完美,漂亮和流畅,但这code已经成为我的主要的游戏循环并创建位图取值每次迭代使得它非常缓慢。我该如何调整图片的大小,因此不会得到像素化,并得到干得快?

找到了解决办法:

 油漆涂料=新的油漆();
paint.setFilterBitmap();
canvas.drawBitmap(位图,矩阵,油漆);
 

解决方案

调整大小位图:

 公共位图getResizedBitmap(位图BM,诠释newHeight,INT newWidth)
{
    INT宽度= bm.getWidth();
    INT高= bm.getHeight();
    浮动scaleWidth =((浮点)newWidth)/宽度;
    浮动scaleHeight =((浮点)newHeight)/身高;
    //创建矩阵用于操作
    字模=新的Matrix();
    //调整位图
    matrix.postScale(scaleWidth,scaleHeight);
    //重新创建新的位图
    位图resizedBitmap = Bitmap.createBitmap(宽多重峰,0,0,宽度,高度,矩阵,假);
    返回resizedBitmap;
}
 

pretty的自我解释:简单地输入原来的位图对象和位图的需要的尺寸,并且此方法将返回给你新位图调整! 可能,这是对您有用。

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

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?

FOUND THE SOLUTION:

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

解决方案

Resizing a Bitmap:

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

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.

这篇关于如何eficiently并在安卓不失品位调整位图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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