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!
Related
I have a PNG image (with transparent background) and I want to draw an outline around its visible pixels only. (See example GIF attached). I want to get this all done in JAVA.
I've read many Q&A over stackoverflow and around the web but I didn't find anything anyway near to my requirements.
P.S: We don't have to draw the same image twice on canvas and use the bottom one as outline, so please don't propose such solutions.
P.P.S: I would be great if this solution lets me draw an outline around all visible objects/elements (images & textviews etc) inside a canvas or inside a layout.
Try using Open CV and drawing contours. It is the closest available solution
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.
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.
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.
I'm trying to create an animation by dynamically generating frames at each step of the animation. Now as I need to run an algorithm to draw pixel by pixel the new frame and I'm using a BufferedImage that I access through its raster data.
However 90% of the time is spent inside java.awt.graphcis.drawImage() that I use to transfer the image into the content of a JFrame.
Is there a more efficient way to draw pixel by pixel inside a graphics object?
Try to use VolatileImage. Much faster than that is hard to do..