Baruch
Baruch

Reputation: 1636

How can I control a Bitmap's transparency?

I am making a 2D game for the Android platform. The character is supposed to move only up and down, so I want to create 2 arrows for movement at the bottom of the screen so the player can move the character up and down. I also want to make sure that they are not blocking the view or that the player can't see what he is doing, so I want them to be about 50% transparent.

How can I control the Bitmap's transparency level? I am using canvas on a SurfaceView and I control it in a thread using lockCanvas and unlockAndPost.

Upvotes: 1

Views: 385

Answers (2)

Romain Guy
Romain Guy

Reputation: 98521

When you draw the bitmap, you pass a Paint as an argument. You can set the transparency level on the Paint by using Paint.setAlpha():

paint.setAlpha(127); //50% translucent
canvas.drawBitmap(bitmap, x, y, paint)

Upvotes: 2

Yogu
Yogu

Reputation: 9455

If you use PNG files instead of BMP files, you can make then semi-transparent. With the free software Inkscape you can import .bmp's (via drag & drop), make them transparent (select the picture and press Ctrl + Shift + F) and export them as png (Ctrl + Shift + E).

Upvotes: 0

Related Questions