有一个白色背景的位图,里面有一个橙色的三角形。我想要做的是将橙色三角形的颜色更改为我想要的颜色,如何使用代码实现此目的?我使用了Porterduff和Color Matrix,但没有得到我想要的结果。
原件:
点击查看图片
我想用代码做什么:
点击查看图片
我不希望白色背景颜色改变。
发布于 2021-03-01 01:27:51
我已经使用此方法来更改位图运行时的颜色。试一试。
public Bitmap replaceColor(Bitmap src,int fromColor, int targetColor) {
if(src == null) {
return null;
}
// Source image size
int width = src.getWidth();
int height = src.getHeight();
int[] pixels = new int[width * height];
//get pixels
src.getPixels(pixels, 0, width, 0, 0, width, height);
for(int x = 0; x < pixels.length; ++x) {
pixels[x] = (pixels[x] == fromColor) ? targetColor : pixels[x];
}
// create result bitmap output
Bitmap result = Bitmap.createBitmap(width, height, src.getConfig());
//set pixels
result.setPixels(pixels, 0, width, 0, 0, width, height);
return result;
}
}在colors.xml中定义from和to颜色
#FB0000
#FFC953并使用上述方法,如下所示。
iv.setImageBitmap(replaceColor(bitmap,getResources().getColor(R.color.red),getResources().getColor(R.color.yell)));https://stackoverflow.com/questions/66411553
复制相似问题