Android 更改图像的颜色
我想更改imageview中存在的图像的颜色。我从位图对象的imageview中获取图像,并在其上应用了colormatrix。 问题是,一旦我改变了图像的颜色,它不会改变之前的颜色, 我想,当我改变颜色时,图像的前一种颜色应该被删除,我选择的任何特定颜色都会被应用 我正在使用下面的代码来做这件事Android 更改图像的颜色,android,image,colors,Android,Image,Colors,我想更改imageview中存在的图像的颜色。我从位图对象的imageview中获取图像,并在其上应用了colormatrix。 问题是,一旦我改变了图像的颜色,它不会改变之前的颜色, 我想,当我改变颜色时,图像的前一种颜色应该被删除,我选择的任何特定颜色都会被应用 我正在使用下面的代码来做这件事 void setImageColor(RGBColor rgbcolor,ImageView view)//,Bitmap sourceBitmap) { view.se
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;
}
声明静态
sourceBitmap
并仅执行一次:Bitmap sourceBitmap=view.getDrawingCache()代码>比方说在活动的onResume()
中(或者当您从ImageView更改图像时)
你的职能应该是:
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);
}
这样,您将保留未更改的图像并对原始图像应用过滤器。声明一个静态sourceBitmap
并仅执行一次:Bitmap sourceBitmap=view.getDrawingCache()代码>比方说在活动的onResume()
中(或者当您从ImageView更改图像时)
你的职能应该是:
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);
}
这样,您将保留未更改的图像,并对原始图像应用过滤器。您对答案有反馈吗?您对答案有反馈吗?