简体   繁体   English

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

[英]Maintaining Image quality when creating scaled bitmap in android

I have an image of dimension 960x800 and I am trying to make it fill the screen. 我有一个尺寸960x800的图像,我试图让它填满屏幕。

The way I have been currently doing it is by loading the full 960x800 bitmap and using source and destination Rect objects. 我目前正在这样做的方法是加载完整的960x800位图并使用源和目标Rect对象。 So far example, my destination rectangle is 480x320 (the dimensions of my screen) and the source rectangle will be 960x800. 到目前为止,我的目标矩形是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);

If I then use a Paint object and set dither true when trying the background to the canvas, the image looks absolutely perfect. 如果我然后使用Paint对象并在将背景设置为画布时将抖动设置为true,则图像看起来绝对完美。

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

Thereby creating an already sized bitmap, elminating the need for any Rect objects. 从而创建一个已经很大的位图,消除了对任何Rect对象的需求。 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. 事实上,如果我不使用上述方法的Paint对象,图像看起来类似于可以看到的图像。 Using the paint object on the scaled bitmap has seemingly no effect. 在缩放位图上使用绘制对象似乎没有任何效果。

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? 我能做些什么来获得相同的图像质量吗?

this is the condition if you want your image to be of width 140nyour height will get resized automatically 这是条件,如果您希望您的图像宽度为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);
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM