3

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.

2
  • Great, I'm glad you figured it out! Since you figured it out on your own, you should add the solution as an answer to this question, and then accept your answer as the correct one. Commented Aug 9, 2011 at 0:22
  • I was going to, but I couldn't add my own answer until at least 8 hours after I ask the question since I am new. I will do that now though. Commented Aug 9, 2011 at 14:08

2 Answers 2

4

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.

Sign up to request clarification or add additional context in comments.

1 Comment

But this requires API level 12. What to do if I have API level 8?
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.