提高 Android 位图上 getpixel() 和 setpixel() 的速度

发布于 2024-10-12 07:22:29 字数 1109 浏览 1 评论 0原文

所有,

在我注意到 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();
}
}

All,

After I noticed how slow getPixel and setPixel are (not sure which one, guess both are not turbocharged) I quickly coded a container for Bitmap that uses int[] array to handle bitmap operations.

Already - its noticeably faster, but this is not enough. Please could you advice how to speed it up further?

My idea is to keep track of what is made "dirty" by the setPixel functions and update only this part of Bitmap when getBitmap() is called ... not clear on how to set the setPixels parameters though (something with offset and stride I guess).

Also - any faster recipe?

Thanks for all help in advance!

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();
}
}

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

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

发布评论

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

评论(2

三寸金莲 2024-10-19 07:22:29

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

直接访问像素数组比通过这些函数访问要快得多。当然,这意味着您必须公开像素,从设计的角度来看这不是很好,但如果您绝对需要可以获得的所有性能,那么这就是正确的方法。

另请参阅 Android 文档中的性能设计

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

For functions as simple as setPixel / getPixel, the function call overhead is relatively large.

It would be a lot faster to access the pixels array directly instead of through these functions. Of course that means you have to make pixels public, which isn't very nice from a design point of view, but if you absolutely need all the performance you can get, this is the way to go.

See also Designing for performance in the Android docs.

If this is still not enough, consider coding your bitmap operations in C++ using the NDK.

温柔女人霸气范 2024-10-19 07:22:29

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

another alternative is using the android ndk. when it comes to that you have very little help but it really increases the speed

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