问题
I use setPixel() to set the pixel to a certain value, but then when I call getPixel on the same pixel right after it, it returns a different value.
It works fine when alpha==255, but any other value it gives a different value;
This was tested on an Asus Transformer, Honeycomb 3.2
int newPixel=Color.argb(alpha, red, green, blue);
if(x==74&&y==86){
Log.w("PuzzleMaker","newPixel:"+newPixel+","+image.getConfig().name()+","+image.isMutable());
}
image.setPixel(x,y,newPixel);
if(x==74&&y==86){
int testPixel=image.getPixel(x, y);
Log.w("PuzzleMaker","testPixel:"+testPixel);
}
Log:
newPixel: 13426418,ARGB_8888,true
testPixel: -16777216
The 2 numbers in the log should be the same.
回答1:
It turned out to have a very simple solution. I figured out that the setPixel() method was multiplying the red,green, and blue values by the alpha, then only setting r,g,b.
Just simply calling image.setHasAlpha(true) fixes this so it sets all 4 values.
回答2:
I think this has to do with some quirks in the way Android handles bitmaps.
Based on your log output, you seem to be setting the pixel to 0x00ccdef2
(which is completely transparent if I'm not mistaken). When you retrieve the pixel, you get -16777216
, which if you look at the documentation for the Color class, is the constant Color.BLACK. Basically, it seems that Android is not saving the transparency of the image. So when you give it a completely transparent color, it just treats it as black.
There is some discussion of a similar problem here, and also an old bug report here.
来源:https://stackoverflow.com/questions/6985819/android-bitmap-setpixel-doenst-work-correctly-set-value-then-read-a-differen