How can i use canvas.save and canvas.restore? [duplicate] - java

This question already has an answer here:
Closed 11 years ago.
Possible Duplicate:
Can i save lots of bitmaps to one bitmap? (2d)
I wonder how canvas.save and canvas.restore really works.
how i want it to work, and how i use it (but doesnt work).
lock the canvas
do some drawing with out unlockandpost
canvas.save() (store the int)
Do some more drawings
Post the canvas
Restore the canvas from step 3
Do some more drawings, repeat from 6 (loop)
What i really need is to save my canvas at a certain stage ( the background), and then draw objects above it, without having to draw the background everytime i want to update my canvas.

Canvas.save() & restore() don't act on the bitmap attached to the canvas... they exist to control aspects of the canvas drawing environment, specifically the current clipping area and the matrix.
You'd use save() and restore if you wanted to, say, draw a rotated sprite. To do that, you'd first save() the current canvas state, then you'd translate() so that the origin - pixel address (0,0) - is where you want the sprite to go, then you'd rotate(), and then you can drawBitmap(). Finally you can restore() the drawing environment back to normal.
So you're basically doomed to draw the background every time. If this is a complex operation, store it in an offscreen bitmap. So long as the background can be drawn in a single operation (drawBitmap, say) performance shouldn't suffer too much.

Ok, so I figured it out.
I can draw my background containing lots of images to one bitmap using canvas, its pretty simple.
First create an empty bitmap with desired int height and int width, this will be the bitmap that you will draw all your tiles too (small images).
Bitmap background = Bitmap.createBitmap(width, heigth, Bitmap.Config.ARGB_4444);
(Not sure about the syntax Bitmap.Config.ARGB_4444 , use tooltip)
Then create a canvas with new Canvas(bitmap), this will make the canvas write to the bitmap.
Canvas canvas new Canvas(background);
Now you can write the canvas as you please, all will be stored in the bitmap for later use.

Related

Canvas Drawing using an image as place holder

I want to have a drawing canvas within my application which will display letter to free draw for practice. Something like the following:
OR
This is the first time I will be implementing this method so a few questions.
Can I have my own background for the canvas? (let's say a chalkboard?)
Do I have to create the letter as image and insert it onto the canvas as Bitmap?
I saw some tutorials but wasn't too clear on how to implement it within app.
I found a solution for you. Having more than 1 Canvas is not the answer (Although may work, I'm still new), but instead draw a seperate Bitmap on top of the first Bitmap (background).
Try this out for size: Overlying Bitmap
You should then be able to edit the overlaying Bitmap without affecting the original Bitmap.
Cheers!

drawing a background element

Current problem is that I am running into framerate issues on my simple android game due to me having to drawing a background for the view every time onDraw() is called. I am currently having to draw around 800 bitmaps on each time invalidate() is called, each of which is an element to a map. I am using a for loop to draw each of the bitmaps.
I was wondering if there was a more efficient method of drawing the bitmaps so that the background could only be drawn once and the player sprite is updated.
I currently tried to only draw the back ground once but the canvas will just overwrite the background leaving the view with a blank canvas and the character sprite.
Thanks in advance for any advice.
You may use View.invalidate method with the dirty Rectangle as input
public void invalidate (Rect dirty)
For more info refer API reference http://developer.android.com/reference/android/view/View.html#invalidate(android.graphics.Rect)

Android Drawing Performance - canvas.drawRect vs Bitmap Drawing

I am writing a particle based game that is mainly built by drawing lots of colored shapes.
Question 1)
For most of the enemy units I am drawing 4 layered rectangles by setting the paint and then drawing the rectangle through the canvas.
I was wondering if it is better to draw using bitmaps, or to draw using the canvas drawing tools? I could easily make a single image of the enemy unit that I wish to draw.
Question 2)
For the images that I have to draw to the screen, I was wondering how I need to load them?
Right now I have tons of .png images loaded like this:
direction1 = BitmapFactory.decodeStream(assetMgr.open("direction1.png"));
I've read that RGB565 is the fasted image type to draw to the screen. Microsoft Paint has some saving options, but for the most part programs only save as a bitmap, not a type of bitmap. If I was to start using that new format would I:
Make new images and use the same loading code.
Use the same images and add something like Bitmap bmp =
Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565); to
convert from the initial loaded format to the RGB565 format.
Make new images and change my loading code.
Thanks for any help! It is very appreciated.
None. It is always better to use OpenGL and the only downside is
that it requires more energy from a battery because it's hardware
accelerated.
RGB565 means that image uses 16 bits so that's the
option you should look for. Don't convert anything, just create them
in the format you will be using.
If the Bitmap aren't moving frame-by-frame, you should try to reduce invalidate() method call. The canvas should only be re-drawn when changes are made and should be updated.

Moving Object/Bitmap in Java for Android

I haven't been able to find much code/tutorials on Moving an object with Threads in Java for Android. I would like some help or code on moving the X and Y position with Threads in Java for Android.
The main hurdle you are facing is that you are thinking of the location of drawing the bitmap being somehow tied to the bitmap itself. You need to track them separately. int draw_x and int draw_y and update those values using your thread or a timer or whatever means you want. Then, in the onDraw method of your view, you want to canvas.translate to the position draw_x,draw_y and then draw your bitmap.
You don't want to "move the bitmap", you want to alter the translation of the canvas when the bitmap is drawn.

Android Canvas.DrawBitmap without blurring/AntiAliasing?

I'm trying to make an android game using sprites, (or very pixelated characters, backgrounds etc.). I draw them on the canvas like so...
matrix.preScale(xrat,yrat);
canvas.drawBitmap(img, matrix, null);
Where img is the Bitmap and the xrat and yrat are the scales.
My problem is that when I test, the Bitmap is blurry or anti-aliased, is there a way to prevent this? The rigid-blocky art style of the game will be ruined if the blocks are blurry.
Any (ANY) help appreciated!
Create a new Paint to use when drawing the bitmaps with the settings:
Paint drawPaint = new Paint();
drawPaint.setAntiAlias(false);
drawPaint.setFilterBitmap(false);
Filtering I believe is on by default and will attempt to smooth out bitmaps when drawn scaled up.

Categories

Resources