2013-07-26 57 views
0

我有(的大小1024×1024)的图像中的int数组(INT []个像素),我使用下面的循环反相一个信道...为什么我的Android应用程序如此缓慢?

int i = 0; 
for (int y = 0; y < H; y++) { 
    for (int x = 0; x < W; x++) { 
     int color = pixels[i]; 
     pixels[i] = Color.argb(Color.alpha(color), 255 - Color.red(color), Color.green(color), Color.blue(color)); 
     i++; 
    } 
} 

这需要在我的新超过1秒Galaxy S4手机。即使在较旧的iPhone上,也可以在一瞬间运行类似的循环。有什么我在这里做错了吗?

如果用“Color.BLUE”替换“Color.argb(Color.alpha(color),255 - Color.red(color),Color.green(color),Color.blue(color))”,它变得更快。

找到解决方法。

如果我用我自己的位运算符,而不是颜色的功能,它会更快...

int i = 0; 
    for (int y = 0; y < H; y++) { 
    for (int x = 0; x < W; x++) { 
     int color = pixels[i]; 
     int red = ((color & 0x00ff0000) >> 16); 
     pixels[i] = (color & 0xff00ffff) | ((255 - red) << 16); 
     //pixels[i] = Color.argb(Color.alpha(color), 255 - Color.red(color), Color.green(color), Color.blue(color)); 
     i++; 
     } 
    } 

回答

0

我认为,如果你更换这段代码利用这一点,将是比较快的

  int w = bitmap.getWidth(); 
     int h = bitmap.getHeight(); 
     int pixels[] = new int[w * h]; 
     bitmap.getPixels(pixels, 0, w, 0, 0, w, h); 
     int n = w * h; 
     for (int i = 0; i < n; i++) { 
      int color = pixels[i]; 
      pixels[i] = Color.argb(Color.alpha(color), 255 - Color.red(color), Color.green(color), Color.blue(color)); 
     } 
     bitmap.setPixels(pixels, 0, w, 0, 0, w, h); 
+0

我可以做这种优化,但这里的瓶颈是颜色操作。所以,整体运行时间仍然差不多。 – Abix

相关问题