提高Android Bitmap上getpixel()和setpixel()的速度
所有,
在我注意到getPixel
和setPixel
有多慢(不确定哪一个,猜两者都不是涡轮增压)后,我快速编写了一个使用int[]
数组来处理位图操作的Bitmap
容器。
已经 – 它明显更快,但这还不够。 请问您能如何进一步加快速度?
我的想法是通过setPixel
函数跟踪“脏”的内容,并在调用getBitmap()
时仅更新Bitmap
这一部分…虽然不清楚如何设置setPixels
参数(具有偏移和步幅的东西)猜测)。
还 – 任何更快的食谱?
提前感谢您的帮助!
import android.graphics.Bitmap; public class DrawableBitmapContainer { private Bitmap image; private int width, height; private int[] pixels; public DrawableBitmapContainer(Bitmap _source ){ image = _source; width = image.getWidth(); height = image.getHeight(); pixels = new int[width*height]; image.getPixels(pixels,0,width,0,0,width,height); } public int getPixel(int x,int y){ return pixels[x+y*width]; } public void setPixel(int x,int y, int color){ pixels[x+y*width]=color; } public Bitmap getBimap(){ image.setPixels(pixels,0,width,0,0,width,height); return image; } public int getWidth(){ return image.getWidth(); } public int getHeight(){ return image.getHeight(); } }
对于像setPixel
/ getPixel
这样简单的函数,函数调用开销相对较大。
直接访问pixels
arrays而不是通过这些函数会快得多。 当然这意味着你必须公开pixels
,从设计的角度来看这并不是很好,但是如果你绝对需要你可以获得的所有性能,那么这就是你要走的路。
另请参阅Android文档中的性能设计 。
如果这还不够,可以考虑使用NDK在C ++中编写位图操作。
另一种选择是使用android ndk。 当谈到你没有什么帮助,但它确实提高了速度