Setting background in a game without using Bitmap - java

I'm making a simple game, like this one, where you have to go through that little space between the 2 lines, but all my lines are descending and you can only move your character left/right at the bottom of the screen.
I've been wandering through a lot of topics (most from -2014) and found out that the best way to draw a background on a cansa is by using a bitmap. The problem is that this takes a lot of resources, my FPS dropping from 40+ to <20.
This is what I found :
canvas.drawBitmap(BitmapFactory.decodeResource(getResources(),R.drawable.big_background),0,0,null);
Is there any better way? Like setting the setContentView to a specific XML activity, knowing that in order for my lines to descend, I need to redraw them 30 times/ sec ?

David has it right: save the Bitmap once and reuse it when you need to draw.
In your Activity, add private Bitmap bg;
In the onCreate(), add bg = BitmapFactory.decodeResource(getResources(),R.drawable.big_background);
You can now do canvas.drawBitmap(bg); without incurring the bitmap load penalty.

Related

One image works fine and other one slow code so much

So I am doing small game in android studio - java. And I was using 400x400 .png image as a ship. It worked well.
Bitmap.createScaledBitmap(BitmapFactory.decodeResource(context.getResources(),R.drawable.playermove),(int)(length),(int)(height),false);
I am using drawBitmap to draw image on screen.
Now I want to use different ship. Only thing that I changed is that instead of this image I use another one that is 150x150 also .png and game becomes so laggy and sloopy.
Length and Height are 1/10 size of the screen. I have 8 ship pictures with similair dimensions and every one make game sloppy.
Any idea why those pictures make everything sloppy and first one doesn't?
You are doing that on the UI thread. You shouldn't!
I'd recommend not to scale Bitmap dynamically, as bitmap scaling is bit expensive, and if you are doing that in onDraw() which would be triggered every ~17ms, doing redundant/expensive work is bad. I'd suggest you to preprocess the image and keep a scaled version of it and just use it to draw every time.
you are not optimizing your images and this is a background work. try to do this in background.
also this post may help you.

Activity with transparent background and blur-filter

I'm currently working on an application where I at one point want to open an Activity that has a transparent background so that I can see the activity below it. This was simple enough and I solved it quick. However, now I want to also add some kind of blur filter so that the activity below my activity seems blurred out.
I have searched around and tried several different solutions but with no luck so I'm trying to add this as a question. Does anyone know of any good way to solve this?
Thanks
There are plenty of libraries that do this. Here's one list of them. Personally I've used BlurView (I hope linking to one of the libraries is not against site rules) but I've had to patch the library a bit, see this issue - the pull request is not in yet. I've also had performance issues with animations.
Basically all the libraries do the same things:
Create Canvas for a Bitmap, with scaled down dimensions (e.g. 4 times smaller), set the transformation matrix to apply this scaling
Render the background of the window to Canvas
Render the root view of the Activity to the Canvas
Blur the Canvas's Bitmap using ScriptIntrinsicBlur
Draw the Bitmap as the background of a View, scaled back up e.g. 4 times
Why the scaling? For performance reasons. It's much faster to draw everything 4 times smaller, and also the blur effect becomes "stronger" due to the upscaling - there's a limit how much ScriptIntrinsicBlur can blur with one pass, and multiple passes slow things down again.
For API < 14, you could use the flag WindowManager.LayoutParams.FLAG_BLUR_BEHIND.
For higher APIS this is no longer supported.
However, you could do this with a view.
Create a Bitmap from your activity's overall layout and Blur that bitmap with whatever method you want. Add(or unhide) a View in your layout that covers everything and you have your problem solved.

Java: How to draw & put text on a bitmap (for low-res sign)

I recently aquired a 112x16 pixel Flip-Dot sign and I want to put cool stuff on it. (Think current temp, playlist items etc.)
The sign has two modes, a rather limited text mode and a bitmap mode.
I know how to put text or a bitmap on the sign, what I'm looking for is tips how to put texts, sprites and simple things like a line on a bitmap that I can then put on the sign.
For example: If I want to show a progress bar of my current playing Item, I would want to draw a few lines/rectangles showing the progress bar, and some text with current and total time above it.
I'm not looking for specific code, but what libraries/projects you would use.
At first glance a BufferedImage looks right to hold the image, but can't find a lot on how to draw on it.
Also, looking for specific tips on low-res bitmap fonts.
I could find a lot on how to draw on the android-specific Canvas, which isn't helping, since this will be something that runs on a raspberry Pi.

Bitmap (or view?) fading

If I display a bitmap as a background, and then draw 3 other, smaller, bitmaps on top, is there a way to fade the background without affecting the other 3 bitmaps?
Basically what I want to do is to move three 'sprite buttons' and a logo image onto the screen, over the top of the game screen, and have the background fade down while the buttons are displayed. When the buttons move off, I want the background to fade back in again.
Any ideas?
Look, this is a design question initially. I need to see if it's possible before I commit.
I have a background. On this I want to show 3 'sprites'. I want to fade the background down without fading the sprites. Is this possible and, if so, how best should it be done?
Considering that the background image will fade and the other 3 will not, is there a reason why they are in the same "container".
If not, then consider having only the background being altered, and the other three images isolated from the altering code.
If they must be drawned together or inside the same bitmap, consider having regions not to alter inside the bitmap, perhaps by clustering the image, and placing identifiers on those regions with a boolean (such as "isAlterable"), then only calling the fade on isAlterable clusters

Trying to make animation on SurfaceView's canvas

I've made a game with a player that looks like this:
I want to make him blink. For this, I've made, with photoshop, a few images that corresponds to the flutter:
Depending on the time, I draw an image or I draw another one, but this takes a lot of memory and makes my app go slow, any alternative to this method? Thanks in advance !
Depending on task it exist few solutions.
1) AnimationDrawable you could just add few your drawable resources. Then you will be able to use this Drawable with ImageView, ProgresBar, etc.
2) If you really want to use SurfaceView, you could merge your res into one mipmap file, and then draw different parts of this mipmap.
3) Performance solution. you could use your mipmap as texture on gl es scene. There is a lot topics about it.
Try to create a unique image containing all the animation states.
It is more expensive to open and modify several small files than one big file.

Categories

Resources