I'm making a simple 2D game for android using Bitmap sprites on a SurfaceView.
From this example, I'm able to load a bitmap and set the black pixels to transparent.
This renders fine and the formerly black pixels are transparent, however if I apply any rotation to the canvas before rendering the bitmap, the transparency is lost and the black pixels are opaquely rendered.
Some code...
In class definition:
private Paint paintSprite;
private Bitmap playerSprite;
In constructor:
paintSprite = new Paint();
BitmapFactory.Options options = new BitmapFactory.Options();
options.inMutable = true;
playerSprite = BitmapFactory.decodeResource(activity.getResources(), R.mipmap.asx27, options);
//Make black parts of the bitmap transparent.
for(int x = 0; x < playerSprite.getWidth(); x++)
{
for(int y = 0; y < playerSprite.getHeight(); y++)
{
if(playerSprite.getPixel(x, y) == Color.BLACK)
{
playerSprite.setPixel(x, y, Color.TRANSPARENT);
}
}
}
In render function:
canvas.save();
canvas.translate(player.GetX() + playerSprite.getWidth() / 2, player.GetY() + playerSprite.getHeight() / 2);
canvas.rotate(player.GetPitch());
canvas.scale(1.0f, -1.0f);
canvas.drawBitmap(playerSprite, -playerSprite.getWidth() / 2, -playerSprite.getHeight() / 2, paintSprite);
canvas.restore();
Can anyone offer an explanation and/or solution?