我有一个Android位图,并且我正在尝试更改图像的色相,因为图像是红色块,所以我想仅通过更改色相将其更改为绿色,但是我似乎无法在任何地方找到任何代码。
谁知道我该怎么做?
画布
好吧,如果您所要做的就是“将红色变为绿色”,则只需切换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。
[This question及其答案为该主题提供了很多启示。
这里是ColorMatrix的technical description:
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;
如果将位图包装在ImageView
中,则有一种非常简单的方法:
ImageView circle = new ImageView(this);
circle.setImageBitmap(yourBitmap);
circle.setColorFilter(Color.RED);
如果您想将其显示在屏幕上,您可能仍希望将其包装在ImageView
中。