开创了Android的缩放位图时保持图像质量 [英] Maintaining Image quality when creating scaled bitmap in android

查看:134
本文介绍了开创了Android的缩放位图时保持图像质量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有尺寸960x800的图像,我试图使它充满屏幕。

I have an image of dimension 960x800 and I am trying to make it fill the screen.

我一直在目前这样做的方式是通过加载完整的960x800位图和使用源和目标矩形的对象。到目前为止,例如,我的目标矩形是小480x320(我的屏幕的尺寸)和源矩形将是960x800。

The way I have been currently doing it is by loading the full 960x800 bitmap and using source and destination Rect objects. So far example, my destination rectangle is 480x320 (the dimensions of my screen) and the source rectangle will be 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);

如果我再使用油漆对象,并设置抖动试图背景画布时真实,图像看起来绝对完美的。

If I then use a Paint object and set dither true when trying the background to the canvas, the image looks absolutely perfect.

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

我的结论是,这可能不是很有效,从而导致不​​必要的工作也可以在每个绘制调用完成的。为了解决这个问题,我想尝试并做到以下几点:

I have concluded that this may not be very efficient, causing unnecessary work to be done on each draw call. To combat this, I want to try and do the following:

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

从而创造一个已经大小的位图,elminating任何矩形对象的需要。但是,这样做的时候,我不能保持相同的图像质量,我可以用上面的方法。事实上,图像看起来类似于可如果我不使用油漆对象与上述方法可以看出。在缩放位图使用描绘对象似乎没有效果。

Thereby creating an already sized bitmap, elminating the need for any Rect objects. However, when doing this I cannot retain the same image quality as I can with the above method. In fact, the image looks similar to that which can be seen if I do not use a Paint object with the aforementioned method. Using the paint object on the scaled bitmap has seemingly no effect.

要给予我的形象看起来如何的例子,它类似于<一个href=\"http://upload.wikimedia.org/wikipedia/commons/thumb/b/be/Dithering_example_undithered_web_palette.png/225px-Dithering_example_undithered_web_palette.png\"相对=nofollow>这个的画面(虽然并不严重)

To give an example of how my image looks, it is similar to this picture (althought not as severe)

有什么我可以做的就是相同的图像质量?

Is there something I can do to get the same image quality?

推荐答案

这是条件,如果你想你的形象是宽度140nyour高度将得到自动调整

this is the condition if you want your image to be of width 140nyour height will get resized automatically

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

这篇关于开创了Android的缩放位图时保持图像质量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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