如何在 Android 中使用双三次插值在画布上绘制和缩放位图?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2517641/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to draw and scale a bitmap on a canvas using bicubic interpolation in Android?
提问by Brian
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 pixelated, if the source image is sightly smaller than the destination rectangle.
我想在比实际尺寸更大的画布上绘制位图。我可以使用 canvas.drawBitmap(bitmap, null, destRect, null); 但是如果源图像比目标矩形小一点,那么质量就会很差,因为结果是像素化的。
How can i draw my bitmap using bilinear or bicubic resampling?
如何使用双线性或双三次重采样绘制位图?
Any help would be appreciated, thanx.
感谢任何帮助,thanx。
回答by Brian
You need to set the FILTER_BITMAP_FLAG flag in your paint.
您需要在油漆中设置 FILTER_BITMAP_FLAG 标志。
canvas.drawBitmap(bitmap, matrix, null); //ugly jaggies unless scale is 1:1, but fast
or
或者
Paint paint = new Paint();
paint.setFilterBitmap();
canvas.drawBitmap(bitmap, matrix, paint); // pretty but slower (not too slow, usually)
The same applies to other (syntactic sugar) forms of the drawBitmapmethod.
这同样适用于该drawBitmap方法的其他(语法糖)形式。
There is no documentation anywhere for most of the Android drawing options and neither is there documentation for the underlying native Skia library that does the rendering. I'd be happier if there were. You can search the Skia source code on Google Code Search. Search for Skia and dig into the raw source; that's the only documentation.
大多数 Android 绘图选项都没有文档,也没有进行渲染的底层原生 Skia 库的文档。如果有我会更开心。您可以在 Google Code Search 上搜索 Skia 源代码。搜索 Skia 并挖掘原始来源;那是唯一的文档。
回答by goRGon
FILTER_BITMAP_FLAG doesn't work for downscaling in most of the cases.
在大多数情况下,FILTER_BITMAP_FLAG 不适用于缩小规模。
Good downscaling algorithm (not nearest neighbor like) consists of just 2 steps (plus calculation of the exact Rect for input/output images crop):
好的缩小算法(不是最近邻)只包含 2 个步骤(加上计算输入/输出图像裁剪的精确 Rect):
- downscale using BitmapFactory.Options::inSampleSize->BitmapFactory.decodeResource()as close as possible to the resolution that you need but not less than it
- get to the exact resolution by downscaling a little bit using Canvas::drawBitmap()
- 使用BitmapFactory.Options::inSampleSize->BitmapFactory.decodeResource()尽可能缩小到您需要的分辨率但不低于它
- 通过使用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/
以下是 SonyMobile 如何解决此任务的详细说明:https://web.archive.org/web/20140228024414/http: //developer.sonymobile.com/2011/06/27/how-to-scale-images-for-你的安卓应用程序/
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/
这是 SonyMobile scale utils 的源代码:https://web.archive.org/web/20140227233706/http: //developer.sonymobile.com/downloads/code-example-module/image-scaling-code-example-安卓版/

