Replace black color in bitmap with red

后端 未结 3 1125
南笙
南笙 2020-12-05 10:36

How can I replace the black color in a bitmap with red (or any other color) programmatically in Android (ignoring transparency)? I can replace the white color in the bitmap

3条回答
  •  -上瘾入骨i
    2020-12-05 11:27

    Get all the pixels in the bitmap using this:

    int [] allpixels = new int [myBitmap.getHeight() * myBitmap.getWidth()];
    
    myBitmap.getPixels(allpixels, 0, myBitmap.getWidth(), 0, 0, myBitmap.getWidth(), myBitmap.getHeight());
    
    for(int i = 0; i < allpixels.length; i++)
    {
        if(allpixels[i] == Color.BLACK)
        {
            allpixels[i] = Color.RED;
        }
    }
    
    myBitmap.setPixels(allpixels,0,myBitmap.getWidth(),0, 0, myBitmap.getWidth(),myBitmap.getHeight());
    

提交回复
热议问题