I'm creating "maps" for a game I'm making, and it's working fine on <=Android 2.1, but 2.2 is causing problems.
I'm not sure if this is a common method, so I'll explain what I'm doing. First, I draw the map which the user is going to see then I draw a map of identical size with nothing but white and pink (0xffFF006E). Anywhere I draw the pink color is an area/boundary I don't want the player to be able to enter/cross.
So, in-game, the regular map is constantly being drawn and the "bordermap" with the pink color is loaded into a Bitmap (but never actually drawn). To see if the player is attempting to cross a border, I check the player's x,y pos against the bordermap:
private boolean overBorder(int x, int y){
//The int 'COLOR_border' is predefined as 0xffFF006E
if(mPlayerDirection == UP && mMapBorders.getPixel(x, y-mPlayerHeight/2) == COLOR_border)return true;
if(mPlayerDirection == DOWN && mMapBorders.getPixel(x, y+mPlayerHeight/2) == COLOR_border)return true;
if(mPlayerDirection == LEFT && mMapBorders.getPixel(x-mPlayerWidth/2, y) == COLO开发者_如何学PythonR_border)return true;
if(mPlayerDirection == RIGHT && mMapBorders.getPixel(x+mPlayerWidth/2, y) == COLOR_border)return true;
//If those fail, false
return false;
}
If that code returns true, I stop the player. Like I said, this works excellently pre-2.2. If anyone has any insight on how to fix 2.2 compatibility I would appreciate it greatly.
Maybe related to http://code.google.com/p/android/issues/detail?id=4710 , and colors being reported as pre-multiplied or post-multiplied. Are you sure your bitmap has an alpha channel? From that post:
I think I may have found the answer to my own problem. I've only tried it with some test code, but it seems it may be the solution. I was calling BitmapFactory.decodeResource(Resources res, int id) to load a bitmap. A call to Bitmap.hasAlpha() returned false. So, I went into my bitmap with Paint.NET, erased some colors, and re-saved. The hasAlpha() method now returns true. The getPixel() and setPixel() method appear to return values more like they did in 1.6. So, I presume that the problem is not in 2.0, but was actually in 1.6 - in that it thought the bitmap had a transparency color set when it didn't. I'll test tonight and report back my findings.
精彩评论