如何在 Android 中使用双三次插值在画布上绘制和缩放位图?

发布于 2024-08-27 02:35:43 字数 164 浏览 7 评论 0原文

我想在画布上绘制比实际尺寸更大的位图。我可以使用 canvas.drawBitmap(bitmap, null, destRect, null);但这会导致质量很差,因为如果源图像比目标矩形小得多,结果就会像素化。

如何使用双线性或双三次重采样绘制位图?

任何帮助将不胜感激,谢谢。

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.

How can i draw my bitmap using bilinear or bicubic resampling?

Any help would be appreciated, thanx.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一生独一 2024-09-03 02:35:43

您需要在绘画中设置 FILTER_BITMAP_FLAG 标志。

canvas.drawBitmap(bitmap, matrix, null); //ugly jaggies unless scale is 1:1, but fast

或者

Paint paint = new Paint();
paint.setFilterBitmap();
canvas.drawBitmap(bitmap, matrix, paint); // pretty but slower (not too slow, usually)

这同样适用于 drawBitmap 方法的其他(语法糖)形式。

大多数 Android 绘图选项都没有文档,也没有执行渲染的底层本机 Skia 库的文档。如果有的话我会更高兴。您可以在 Google 代码搜索上搜索 Skia 源代码。搜索 Skia 并挖掘原始来源;这是唯一的文档。

You need to set the FILTER_BITMAP_FLAG flag in your paint.

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 drawBitmap method.

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.

情仇皆在手 2024-09-03 02:35:43

在大多数情况下,FILTER_BITMAP_FLAG 不适用于缩小尺寸。

良好的缩小算法(不是像最近邻那样)仅包含 2 个步骤(加上输入/输出图像裁剪的精确矩形的计算):

  1. 使用 BitmapFactory.Options::inSampleSize->BitmapFactory.decodeResource( )尽可能接近您需要的分辨率,但不少于
  2. 通过使用 Canvas::drawBitmap() 稍微缩小尺寸来达到精确分辨率 >

以下是 SonyMobile 如何解决此任务的详细说明: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/20140227233706/http://developer.sonymobile.com/downloads/code-example-module/image-scaling-code-example-for-android/

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/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文