Android Question ImageView Speed Question - LoadBitmap vs changing background colour?

VBAPro

New Member
Licensed User
Longtime User
Please correct me if I'm wrong. I am 99% done building my game app, and I'm having some timing issues.

Basically, I am having some images in ImageViews move randomly around the screen, in time to music. Imagine, if you will, ducks and hawks in foreground ImageViews with transparent backgrounds, so that a background image of a forest lake is behind them.

The music starts when the gameplay scene begins, and the images are moved by the Tick event of a very precisely set timer. There is no ongoing loop. I can have this run on autopilot for over two minutes at a time without missing a beat. So far so good.

However, if I tap on an ImageView, it is supposed to trigger an event which loads another image into that ImageView using the LoadBitmap function.

Say for example that the player shot a hawk who was going to grab a duck, and now I need to display that same hawk in a square with a red background in order to confirm to the player that they shot the hawk successfully. Then they get points added to their score.

This works, but the event with the LoadBitmap function which loads the image of the hawk with the red background takes too long - and after a few of these events, everything falls out of rhythm with the music, and I can't have that.

But the other image only has a different background colour - red instead of transparent. The hawk stays the same.

I wanted to ask this here before actually trying it because it would require a lot of re-work.

Would it be better instead to have two ImageViews with exact same dimensions on top of one another - where the top ImageView would contain the hawk with a transparent background, and the bottom ImageView would contain nothing - it would just have a transparent background - and then when the hawk gets shot, keep the top image intact, and just have the event change the background colour in the bottom ImageView from transparent to red?

I mean - would this be any faster than having just one ImageView for the hawk and invoking LoadBitmap as I do now? If it is faster, I would be happy to try it.

Alternatively - could I use a new Thread for some of the calculations, even though the calculations are very simple? Would Threading help in this case?

Thanks in advance for any help.
 

stevel05

Expert
Licensed User
Longtime User
How many bitmaps need to be changed, if it's only a few, I would be tempted to try pre-loading the bitmaps, then just swapping them, although it will still need to draw the image, it won't need to load them from the memory card.

I haven't measured it, but I would imagine that doing a background fill would be considerably quicker than loading a new bitmap.

It wouldn't be too difficult to set up a test to measure the timings and see. You could also potentially change the Alpha of the background too, so that could be another test, although a quick look at the documentation suggests it may not be that simple. See http://developer.android.com/reference/android/view/View.html#setAlpha(float)
 
Upvote 0

Informatix

Expert
Licensed User
Longtime User
I don't think that writing your game with standard views is a good idea. You should better use one of the libraries dedicated to that. That being said, my BetterImageView library should help you to solve a part of your problems as it can show an image in the foreground and an image in the background. Moreover you have a function to alter colors, so you do not need to load another bitmap with a different color. And if I can give another advice: you should never load anything during the game. As the disk access duration is unpredictable, any synchronization will be difficult. Most games load all the resources they need before the player starts to play (and that allows to check whether the device has enough memory to run the game).
 
Upvote 0

VBAPro

New Member
Licensed User
Longtime User
Thanks a lot, guys! I will definitely pre-load all of my images before the gameplay scene starts - that should solve the problem. :cool:
 
Upvote 0
Top