2014-03-05 24 views
1

在我的應用程序中,我有一個搜索欄,通過滑動用戶可以增加或減少圖像的亮度。我已經完成了這項工作,但問題在於它顯示的速度很慢,滑動搜索欄後大約需要3-4秒才能顯示對圖像的影響。下面是我已經實現的代碼,任何人都可以告訴我該怎麼做才能使這種效果在圖像上平滑。如何在android中的圖像實現亮度?

public static Bitmap doBrightness(Bitmap src, int value) { 
    // image size 
    int width = src.getWidth(); 
    int height = src.getHeight(); 
    // create output bitmap 
    Bitmap bmOut = Bitmap.createBitmap(width, height, src.getConfig()); 
    // color information 
    int A, R, G, B; 
    int pixel; 

    // scan through all pixels 
    for (int x = 0; x < width; ++x) { 
    for (int y = 0; y < height; ++y) { 
    // get pixel color 
    pixel = src.getPixel(x, y); 
    A = Color.alpha(pixel); 
    R = Color.red(pixel); 
    G = Color.green(pixel); 
    B = Color.blue(pixel); 

    // increase/decrease each channel 
    R += value; 
    if (R > 255) { 
    R = 255; 
    } else if (R < 0) { 
    R = 0; 
    } 

    G += value; 
    if (G > 255) { 
    G = 255; 
    } else if (G < 0) { 
    G = 0; 
    } 

    B += value; 
    if (B > 255) { 
    B = 255; 
    } else if (B < 0) { 
    B = 0; 
    } 

    // apply new pixel color to output bitmap 
    bmOut.setPixel(x, y, Color.argb(A, R, G, B)); 
    } 
    } 

    // return final image 
    return bmOut; 
} 
+0

你有沒有嘗試過創建一個AsyncTask在不同的線程中執行這些計算?我不認爲你應該在UI線程上這樣做。 – Demoric

+0

是的,當我執行AsyncTask來執行此操作時,我必須等到我的異步任務執行此操作時,我只想讓它平滑,用戶不應該等待,同時增加或減少亮度 – rahul

回答

0

你走在Java代碼的單個線程的圖像中的每個像素以及使用Color方法分解顏色成其組成部分。這將是RenderScript中的一件好事。 RS將把操作卸載到DSP或GPU(如果您的設備支持)或者在CPU上並行操作。有關RenderScript的基本用法和背景,請參閱this talk

相關問題