关于ColorFilter()函数的一个简短问题;我试图用新的颜色替换图像的特定颜色:
默认值:

结果:

所以在这个例子中,我只想用蓝色代替红色。但是不要修改这个图像的黑色。
目前,Iam使用以下代码:
int color = Color.parseColor("#0000FF"); iv1.setColorFilter(color, PorterDuff.Mode.SRC_ATOP);
根据对"PorterDuff“模式的描述,我应该使用"SRV_ATOP”。但是,我应该如何使用这种模式,以便只有红色才会被替换?
发布于 2018-01-30 08:36:36
我现在找到了一种完美的方法。因此,对于所有面临相同probleme...here的人来说,这是对我有用的代码:
//Decode *.png file to Bitmap
Bitmap Bitmap_temp = BitmapFactory.decodeResource(getResources(), R.drawable.image_1);
Bitmap Bitmap_final = Bitmap_temp.copy(android.graphics.Bitmap.Config.ARGB_8888, true);
//Get Pixel and change color if pixel color match
int [] allpixels = new int [Bitmap_final.getHeight() * Bitmap_final.getWidth()];
Bitmap_final.getPixels(allpixels, 0, Bitmap_final.getWidth(), 0, 0, Bitmap_final.getWidth(), Bitmap_final.getHeight());
for(int i = 0; i < allpixels.length; i++)
{
if(allpixels[i] == Color.parseColor("#fff000"))
{
allpixels[i] = Color.parseColor("#0D0D0D");
}
}
Bitmap_final.setPixels(allpixels,0,Bitmap_final.getWidth(),0, 0, Bitmap_final.getWidth(),Bitmap_final.getHeight());
//Set Bitmap to ImageView
iv_image1.setImageBitmap(Bitmap_final);https://stackoverflow.com/questions/48476265
复制相似问题