[英]change the color of image android
I want to change the color of image that exist in imageview. 我想更改imageview中存在的图像的颜色。 i got the image from imageview in Bitmap object and applied colormatrix on it.
我从位图对象的imageview中获取图像,并在其上应用了colormatrix。 The problem is that , once i change the color of image, it doesn't change bt override the previous color, I want that , when i change the color, the previous color of image should be removed and any specific color that i choose is applied.
问题是,一旦我更改了图像的颜色,它就不会更改bt覆盖先前的颜色,我希望当我更改颜色时,应该删除图像的先前的颜色,并且我选择的任何特定颜色是应用。
i am using the following code to do that... 我正在使用以下代码来做到这一点...
void setImageColor(RGBColor rgbcolor,ImageView view)//,Bitmap sourceBitmap)
{
view.setDrawingCacheEnabled(true);
Bitmap sourceBitmap = view.getDrawingCache();
if(sourceBitmap!=null)
{
float R = (float)rgbcolor.getR();
float G = (float)rgbcolor.getG();
float B = (float)rgbcolor.getB();
Log.v("R:G:B",R+":"+G+":"+B);
float[] colorTransform =
{ R/255f, 0, 0, 0, 0, // R color
0, G/255f, 0, 0, 0, // G color
0, 0, B/255f, 0, 0, // B color
0, 0, 0, 1f, 0f
};
ColorMatrix colorMatrix = new ColorMatrix();
colorMatrix.setSaturation(0f); //Remove Colour
colorMatrix.set(colorTransform);
ColorMatrixColorFilter colorFilter = new ColorMatrixColorFilter(colorMatrix);
Paint paint = new Paint();
paint.setColorFilter(colorFilter);
Bitmap mutableBitmap = sourceBitmap.copy(Bitmap.Config.ARGB_8888, true);
view.setImageBitmap(mutableBitmap);
Canvas canvas = new Canvas(mutableBitmap);
canvas.drawBitmap(mutableBitmap, 0, 0, paint);
}
else
return;
}
Declare a static sourceBitmap
and do this only once: Bitmap sourceBitmap = view.getDrawingCache();
声明一个静态
sourceBitmap
并仅执行一次: Bitmap sourceBitmap = view.getDrawingCache();
let's say in onResume()
of your activity (or when you change the image from ImageView). 比方说您的活动的
onResume()
(或从ImageView更改图像时)。
And your function should be: 您的功能应该是:
void setImageColor(RGBColor rgbcolor, ImageView view, Bitmap sourceBitmap) {
if (sourceBitmap == null) return;
float r = (float) rgbcolor.getR(),
g = (float) rgbcolor.getG(),
b = (float) rgbcolor.getB();
Log.v("R:G:B", r + ":" + g + ":" + b);
float[] colorTransform =
{
r/255, 0 , 0 , 0, 0, // R color
0 , g/255, 0 , 0, 0, // G color
0 , 0 , b/255, 0, 0, // B color
0 , 0 , 0 , 1, 0
};
ColorMatrix colorMatrix = new ColorMatrix();
colorMatrix.setSaturation(0f); // Remove colour
colorMatrix.set(colorTransform);
ColorMatrixColorFilter colorFilter = new ColorMatrixColorFilter(colorMatrix);
Paint paint = new Paint();
paint.setColorFilter(colorFilter);
Bitmap mutableBitmap = sourceBitmap.copy(Bitmap.Config.ARGB_8888, true);
view.setImageBitmap(mutableBitmap);
Canvas canvas = new Canvas(mutableBitmap);
canvas.drawBitmap(mutableBitmap, 0, 0, paint);
}
This way you will hold the unaltered image and apply the filters on the original. 这样,您将保留未更改的图像并将滤镜应用于原始图像。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.