简体   繁体   English

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

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

I want to draw a bitmap on a canvas with bigger size than it is. 我想在尺寸大于它的画布上绘制位图。 I can use canvas.drawBitmap(bitmap, null, destRect, null); 我可以使用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. 任何帮助将不胜感激,thanx。

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 drawBitmap method. 这同样适用于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. 大多数Android绘图选项都没有任何文档,也没有提供渲染的底层本机Skia库的文档。 I'd be happier if there were. 如果有的话,我会更开心。 You can search the Skia source code on Google Code Search. 您可以在Google代码搜索中搜索Skia源代码。 Search for Skia and dig into the raw source; 搜索Skia并挖掘原始资源; that's the only documentation. 这是唯一的文件。

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个步骤(加上计算输入/输出图像裁剪的精确矩形):

  1. downscale using BitmapFactory.Options::inSampleSize->BitmapFactory.decodeResource() as close as possible to the resolution that you need but not less than it 使用BitmapFactory.Options :: inSampleSize-> BitmapFactory.decodeResource()的缩减尽可能接近您需要但不低于它的分辨率
  2. get to the exact resolution by downscaling a little bit using Canvas::drawBitmap() 通过使用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-您-Android的应用程序/

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-换的Android /

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

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