我有一个Android位图,我试图改变图像的色调,因为图像是一个红色块,我想通过改变色调将该块更改为绿色,但我似乎找不到任何代码。
有人知道我怎么做这个吗?
画布如果您将位图包装在ImageView
中,有一个非常简单的方法:
ImageView iv = new ImageView(this);
iv.setImageBitmap(yourBitmap);
iv.setColorFilter(Color.RED);
如果你想在屏幕上显示它,你可能想把它包装在ImageView
中。
好吧,如果你所追求的是"将红色变为绿色",你可以只切换R和G颜色组件。很原始,但也许能帮你。
private Bitmap redToGreen(Bitmap mBitmapIn)
{
Bitmap bitmap = mBitmapIn.copy(mBitmapIn.getConfig(), true);
int []raster = new int[bitmap.getWidth()];
for(int line = 0; line < bitmap.getHeight(); line++) {
bitmap.getPixels(raster, 0, bitmap.getWidth(), 0, line, bitmap.getWidth(), 1);
for (int p = 0; p < bitmap.getWidth(); p++)
raster[p] = Color.rgb(Color.green(raster[p]), Color.red(raster[p]), Color.blue(raster[p]));
bitmap.setPixels(raster, 0, bitmap.getWidth(), 0, line, bitmap.getWidth(), 1);
}
return bitmap;
}
我相信你不会找到一个简单的"色调"拨号盘来调整你的图像的颜色。
最接近的近似(并且应该工作得很好)是使用ColorMatrix。
这个问题和它的答案对这个问题很有帮助。
下面是ColorMatrix的技术描述:
ColorMatrix is a 5x4 matrix for transforming the color+alpha components of a Bitmap.
The matrix is stored in a single array, and its treated as follows:
[ a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t ]
When applied to a color [r, g, b, a], the resulting color is computed as (after clamping)
R' = a*R + b*G + c*B + d*A + e;
G' = f*R + g*G + h*B + i*A + j;
B' = k*R + l*G + m*B + n*A + o;
A' = p*R + q*G + r*B + s*A + t;