How to draw and scale a bitmap on a canvas using bicubic interpolation in Android?

后端 未结 2 1619
野趣味
野趣味 2021-02-02 14:15

I want to draw a bitmap on a canvas with bigger size than it is. I can use canvas.drawBitmap(bitmap, null, destRect, null); but that gives a poor quality, as the result is pixel

2条回答
  •  不知归路
    2021-02-02 15:03

    FILTER_BITMAP_FLAG doesn't work for downscaling in most of the cases.

    Good downscaling algorithm (not nearest neighbor like) consists of just 2 steps (plus calculation of the exact Rect for input/output images crop):

    1. downscale using BitmapFactory.Options::inSampleSize->BitmapFactory.decodeResource() as close as possible to the resolution that you need but not less than it
    2. get to the exact resolution by downscaling a little bit using Canvas::drawBitmap()

    Here is detailed explanation how SonyMobile resolved this task: https://web.archive.org/web/20140228024414/http://developer.sonymobile.com/2011/06/27/how-to-scale-images-for-your-android-application/

    Here is the source code of SonyMobile scale utils: https://web.archive.org/web/20140227233706/http://developer.sonymobile.com/downloads/code-example-module/image-scaling-code-example-for-android/

提交回复
热议问题