提高getpixel的速度(),并与setPixel()在Android位图 [英] improving speed of getpixel() and setpixel() on Android Bitmap

查看:1756
本文介绍了提高getpixel的速度(),并与setPixel()在Android位图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所有,

在我发现有多慢 getPixel setPixel 的(不知道哪一个,估计两者都没有涡轮增压)我赶紧codeD的容器位图使用 INT [] 阵列来处理位图操作。

已经 - 其明显快,但这是不够的。请你能指点如何进一步加快步伐?

我的想法是跟踪哪些是由脏由 setPixel 的功能和更新只有这部分位图 getBitmap()被称为......没有关于如何设置的setPixels 参数虽然明确的(东西与胶印和步幅我猜)。

另外 - 任何更快配方?

感谢事先所有的帮助!

 进口android.graphics.Bitmap;

公共类DrawableBitmapContainer {
私人位图图像;
私人诠释的宽度,高度;
私人INT []像素;
公共DrawableBitmapContainer(位图_source){
    图像= _source;
    宽度= image.getWidth();
    身高= image.getHeight();
    像素=新INT [宽*高]。
    image.getPixels(像素,0,宽度,0,0,宽度,高度);
}
公众诠释与getPixel(INT X,int y)对{
    返回像素[X + Y *宽]
}
公共无效与setPixel(INT X,INT Y,INT的颜色){
    像素[X + Y *宽=颜色;
}
公共位图getBimap(){
    image.setPixels(像素,0,宽度,0,0,宽度,高度);
    返回形象;
}
公众诠释的getWidth(){
    返回image.getWidth();
}
公众诠释的getHeight(){
    返回image.getHeight();
}
}
 

解决方案

有关功能简单,只要 setPixel / getPixel ,函数调用开销是相当大的。

这将是快了很多访问像素阵列,而不是直接通过这些功能。当然,这意味着你必须让像素公开,这是不是从外观设计上来看很不错,但如果你确实需要的所有性能,你可以得到,这对是要走的路。

参见设计在Android的文档效果

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

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

解决方案

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.

这篇关于提高getpixel的速度(),并与setPixel()在Android位图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆