user700508
user700508

Reputation: 81

Making a .bmp file have some transparency

I am making an android 2d game and ran into a problem. I made a .bmp file with transparency in paint.net and exported it to eclipse. It is the cross hare that should be transparent in the center so that you see where your character is going. Unless I exported it incorrectly, i was unable to make my game have some transparency in it, in the correct areas.

Here is the code I added

...
  Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
...
  BitmapFactory.Options options = new BitmapFactory.Options();
       options.inPreferredConfig = Bitmap.Config.ALPHA_8;

  Resources res = context.getResources();
  crosshair = BitmapFactory.decodeResource(res, R.drawable.crosshare,options);
....
  c.drawBitmap(crosshair, x, y, paint);

c is the canvas and the first 5 lines of code are in the constructor of my crosshare class.

Thanks

Upvotes: 0

Views: 726

Answers (2)

DRiFTy
DRiFTy

Reputation: 11369

Have you tried setting the paint alpha? paint.setAlpha(100);... Also if that doesn't work, which I dont think it does on bitmaps, try using a BitmapDrawable(Resource res). This will allow you to call setAlpha() directly on the Drawable. You shouldnt have to change much to use this.

Upvotes: 0

darioo
darioo

Reputation: 47213

Converting your bmp file into png should solve your problem. Not only do png files have natural support for transparency, they are also smaller than bmp files.

Oh, and png is an open format.

Upvotes: 1

Related Questions