在我的应用程序中,我有一个搜索栏,通过滑动它,用户可以增加或减少图像的亮度。我已经完成了这项工作,但问题是显示速度非常慢,在滑动搜索栏后显示效果大约需要3-4秒。下面是我实现的代码,任何人都可以告诉我,我该怎么做才能使这个效果在图像上平滑。
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 :(得分:0)
您将图像中的每个像素都放在单个Java代码线程中,并使用Color
方法将颜色分解为其组成部分。这将是RenderScript
中要做的事情的一个很好的候选人。 RS会将操作卸载到DSP或GPU(如果设备支持)或在CPU上并行化。有关RenderScript的基本用法和背景,请参阅this talk。