【问题标题】:improving speed of getpixel() and setpixel() on Android Bitmap提高 Android Bitmap 上 getpixel() 和 setpixel() 的速度
【发布时间】:2011-01-17 17:11:19
【问题描述】:

全部,

在我注意到getPixelsetPixel 有多慢(不确定是哪一个,猜想两者都不是涡轮增压的)之后,我迅速编写了一个用于Bitmap 的容器,该容器使用int[] 数组来处理位图操作。

已经 - 它明显更快,但这还不够。请您建议如何进一步加快速度?

我的想法是跟踪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();
}
}

【问题讨论】:

  • 我希望您意识到您的位图占用了 2 倍的内存占用,而且您的 setpixel 也不起作用。该数组只是实际位图中的图像矩阵的副本。因此,通过更改其中的值,在您将该缓冲区提交回位图对象之前,它们不会被持久化。
  • 我理解它的内存是我的两倍(值得我使用的速度),但我对你的 setpixel 不起作用的评论感到迷惑 - 如果我通过 getBitmap 方法访问位图,它就可以工作,那就是我想要什么。
  • 确保您以内存占用换取性能,如果您必须转换较大位图上的每个像素,效果会非常好

标签: java android performance bitmap


【解决方案1】:

对于setPixel/getPixel这样简单的函数,函数调用开销比较大。

直接访问pixels 数组而不是通过这些函数会快很多。当然,这意味着您必须将pixels 公开,从设计的角度来看这不是很好,但如果您绝对需要您可以获得的所有性能,这就是要走的路。

另请参阅 Android 文档中的 Designing for performance

如果这还不够,请考虑使用 NDK 在 C++ 中对位图操作进行编码。

【讨论】:

  • 谢谢 - 有没有办法定义一个“宏”,让我使用 getPixel(x,y)
【解决方案2】:

另一种选择是使用 android ndk。说到这一点,你几乎没有什么帮助,但它确实提高了速度

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-02
    • 1970-01-01
    • 2011-07-07
    • 2012-02-11
    • 2018-01-19
    • 1970-01-01
    相关资源
    最近更新 更多