0

I have a bitmap which I am creating like so.....

myBitmap=BitmapFactory.decodeResource(res, R.drawable.picture, options);

I'm then scaling it using Bitmap.createScaledBitmap

then when I'm drawing it I'm re-colouring it like this:

c.drawBitmap(myScaledBitmap, 0, 0, myPaint);

This works, but performance takes a huge hit as I'm re-colouring in realtime.

What I'm now trying to do is create a different colour bitmap before the loop starts running, I thought this would be quite a simple thing to do, but I can't work it out and would appreciate any pointers.

The only other way would be for me to actually create different .png's to start with but that would be a bit of a pain so there must be a way to do this in code? Thanks.

2
  • Nothing much could do, the bitmap handling in the Android SDK is already done in native code. If you need real time, you should consider GLES2.0 and program your own Shader
    – xandy
    Jan 31, 2013 at 2:20
  • Thanks, I don't need real-time, I was only doing it in real time because it was the only way I knew how :-)
    – Zippy
    Jan 31, 2013 at 2:31

1 Answer 1

0

It isn't too complicated. You have most of it. Outside your loop, you can almost just use the code you already have. I think something like this should work:

Bitmap coloredBitmap = Bitmap.createBitmap(myScaledBitmap.getWidth(), myScaledBitmap.getHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(coloredBitmap);
canvas.drawBitmap(myScaledBitmap, 0, 0, myPaint);

Now just use coloredBitmap in the loop.

4
  • Thank you @kabuko, I'm getting there. A couple of things, I don't get a 'getIntrinsicWidth()' method available to me, all I get is getWidth() or getScaledWidth() - same with height. Also, when creating this new bitmap, where does it state what the source bitmap should be? Thanks again for your help.
    – Zippy
    Jan 31, 2013 at 3:42
  • Oops, my mistake, corrected now. Was thinking of a different class. You're creating a new blank bitmap with no source then drawing on it.
    – kabuko
    Jan 31, 2013 at 3:48
  • If you need it in RGB565, then for your new bitmap create it with that config too.
    – kabuko
    Jan 31, 2013 at 18:28
  • thanks for this @kabuko, I'm getting there, but when I draw the new bitmap, it has a black square around it. I thought I had converted my original bitmaps are RGB565 but I now realise that I couldn't have, as they still had their alpha channel - so if they still have the alpha channel, are they then 32bit format? What is the lowest format I can convert to while still retaining the alpha transparency channel? - Any suggestions would be a great help, thanks.
    – Zippy
    Jan 31, 2013 at 18:34

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.