On the fly bitmap editing in Android - android

I'm creating a scene for a game.
There's a background which is a bitmap and player objects. I want to have the player objects to be able to "eat" away at the background with a transparent color as they move around the board. Basically if an object is at a certain point a circle around that object is alpha'ed out of the bitmap. The edits of the bitmap need to persist.
What's the best way to accomplish this? I was thinking of modifying the pixels for the bitmap using Bitmap.setPixel but that would probably be very expensive.

I figured it out.
I need to basically use Canvas's draw functions which are applied to a bitmap.
IE
Canvas.drawBitmap()

Related

Draw on canvas using bitmap pixels?

I have an app that allows the user to draw grids of circles, rectangles, arrows, and paint. All of these are drawn on bitmaps which are in turn drawn on my main canvas. I have it structured this way because the users can zoom/pan on the main canvas (via a main bitmap) as well as move/scale/rotate each drawn object. The problem is that all these bitmaps eat up all of my memory, so I have been trying to find a better solution.
LRUcache is out of the question because I need all of the bitmaps every time the onDraw method is called, which is every time something new is drawn.
I have seen the Bitmap.getPixels() function and the Canvas.drawPoints(); function. Is there a way to use these together so I can get rid of my bitmaps? What I envision is creating a bitmap, drawing the object on it, extracting the pixels from that bitmap, and then recycling the bitmap to free up that memory. Is that feasible?

Drawing to a new Bitmap and then using that in my onDraw

I would like to display several rings with different coloured sections (see below). The colour of the sections, however, cannot be know in advance, so I will need to draw these dynamically.
I know I could draw directly to the canvas but, once I have them, I would like to animate these rings, rotate them, have them overlap etc. It seemed, therefore, that the easiest and possibly least expensive approach would be to create them in advance, in memory, as transparent pngs and then just draw them in onDraw.
My problem is the only methods I can find to do this are setPixel. Is there not a way I could use drawing tools, like in Canvas, to draw to an empty bitmap, once, then use that bitmap with my canvas in onDraw?
I feel like I am missing a piece in the puzzle. Any help would be greatly appreciated.
You can create a Bitmap that is the size you want the ring to be and then create a Canvas the same size. Call setBitmap() on the Canvas and it will draw on to that for you. Then you can build your circle and have a bitmap to hold onto and use just like any other resource.

Open GL and Canvas which method is more suitable for making custome brushes in drawing application?

I want to make brushes displayed in below image for drawing application. Which is a suitable method - Open GL or Canvas & How can we implement it?
I'd say Canvas, as you'll want to modify an image. OpenGLES is good for displaying images, but does not (as far as I know) have methods for modifying its textures (unless you render to a texture that then render to screen with some modifications, which is not always so effective).
Using the Canvas you will have the methods for drawing your brush-strokes onto the Bitmap you're painting on, in GLES you would have to modify a texture (by using a canvas) and then upload that to the GPU again, before it could be rendered, and the rendering would most likely just consist of drawing a square with your texture on it (as the fillrate for most mobile GPUs are quite bad, you don't want to draw the strokes separately).
What I'm trying to say is; The most convenient way to let the user draw on an openGLES surface would be by creating a texture by drawing on a Canvas.
But, there might still be some gain in using GL for drawing, as the Canvas-operations can be performed off-screen, and you can push this data to a gl-renderer to (possibly) speed up the on-screen drawing.
However; if you are developing for Android 3.x+ you should take a look at RenderScript, (which I personally have never had a chance to use), but seems like it would be a good solution in this case.
Your best solution is going to be using native code. That's how Sketchbook does it. You could probably figure out how by browsing through the GIMP source code http://www.gimp.org/source . Out of Canvas vs OpenGL, Canvas would be the way to go.
It depends. if you want to edit the image statically, go with canvas. But if you want after brushing the screen, to have the ability to edit, scale, rotate, it would be easier with opengl.
An example with opengl: Store the motion the user do with touchs. create a class that store a motion and have fields for size, rotation etc. to draw this class, just make a path of the brush image selected following the stored motion.

Canvas Dynamically change a bitmap's z-index

I am creating an Android app and in my app I have a canvas which I draw numerous bitmaps to the canvas via the canvas.drawBitmap () function. From my understanding the z-index on these bitmaps are set based on the order in which they are being drawn to the canvas. What I am trying to figure out is after drawing these bitmaps if I can dynamically change the z-index on a bitmap to push it to the top? This seems like a very simple problem, but I have had not luck in finding a solution yet.
Not really possible: after you call drawBitmap the contents of the bitmap are rendered onto the canvas, but the canvas does not store any references to the original bitmap, it only stores the results of applying the bitmap's contents to the canvas. There is no way to dynamically say that bitmap you drew 1st out of 50, I want you to make that the 50th bitmap and automatically redraw every single other bitmap to reflect the change.
So you'll need to order your drawing operations before hand.

How to play animations in a SurfaceView in Android

I have a SurfaceView which is currently drawing a series of Bitmaps. These bitmaps can easily be manipulated geometrically (scale/rotate/translate/etc). An example would be a ball which is bouncing, which may just use translate/rotate operations. The ball is just a single bitmap.
I wish to replace this 'ball' Bitmap with a sequence of Bitmaps, for example if the ball were to explode in a flurry of fireworks, I have the individual frames and I wish to string them together.
Without having to manually cycle through them on each onDraw() call in my SurfaceView, how can I go about this? I know in normal conditions I can just make an AnimationDrawable using the frames in an XML file, bind it to an ImageView (set as background) then call it. However, I am using a SurfaceView for displaying all my graphics on screen, not ImageViews. I have read you can overlay other views on top of SurfaceViews, but I am unsure that I could get the exact position required. For example, the ball may explode at X=65 Y=21 on my SurfaceView, how could I translate this exact co-ordinate to my ImageView?
Any help or tips on alternative methods would be greatly appreciated.
If you want to keep it more separated, you can create another class that handles this animation and keeps track of which frame it's on and the bitmaps to use then delegate drawing of the sprite to this class in your SurfaceView's onDraw.

Categories

Resources