2013-07-26 55 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

相關問題