在android中创建缩放位图时保持图像质量

2024-01-01

我有一张尺寸为 960x800 的图像,我试图让它填满屏幕。

我目前的做法是加载完整的 960x800 位图并使用源和目标Rect对象。到目前为止,我的目标矩形为 480x320(屏幕尺寸),源矩形为 960x800。

background = BitmapFactory.decodeResource(getResources(), R.drawable.background, null);
destRect = new Rect(0,0, screenWidth, screenHeight);
sourceRect = new Rect(0,0, background.getWidth(), background.getHeight());
...
c.drawBitmap(background, sourceRect, destRect, backgroundPaint);

如果我然后使用Paint当尝试画布背景时,对象并设置抖动为真,图像看起来绝对完美。

Paint backgroundPaint = new Paint();
backgroundPaint .setDither(true);

我得出的结论是,这可能不是很有效,导致每次绘图调用都完成不必要的工作。为了解决这个问题,我想尝试执行以下操作:

background = BitmapFactory.decodeResource(getResources(), R.drawable.background, null);
background = Bitmap.createScaledBitmap(background, display.getWidth(), display.getHeight(), true);

从而创建一个已经调整大小的位图,无需任何Rect对象。但是,执行此操作时,我无法保持与使用上述方法相同的图像质量。事实上,该图像看起来与我不使用时所看到的类似。Paint对象与上述方法。在缩放位图上使用绘制对象似乎没有效果。

举个例子来说明我的图像的外观,它类似于this http://upload.wikimedia.org/wikipedia/commons/thumb/b/be/Dithering_example_undithered_web_palette.png/225px-Dithering_example_undithered_web_palette.png图片(虽然没那么严重)

我可以做些什么来获得相同的图像质量吗?


如果您希望图像宽度为 140,则这是条件,您的高度将自动调整大小

    Bitmap originalBitmap = BitmapFactory.decodeFile(getResources(), R.drawable.background, null);
    Bitmap bitmap=originalBitmap;
    if (bitmap !=null) {
        int h = bitmap.getHeight();
        int w = bitmap.getWidth();
        h = 140*h/w;
        w = 140;
        bitmap = Bitmap.createScaledBitmap(bitmap, w, h, true);
        setIconImage(bitmap);
    }
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在android中创建缩放位图时保持图像质量 的相关文章

随机推荐