I am fairly new with android development. I am trying to take a 8x8 array of integers and make a 8x8 square (64 squares) using canvas and bitmap, a picture below shows what I want to accomplish. Each square would correspond to the index of the integer array, and the color of the square will change depending on the integers(0-255).
Currently, I am just trying to draw the layout of my application, but I am stuck on drawing the array of squares using canvas and bitmap. I have looked at different sources and the following two seems like very close to what I want to do.
source 1: I declared a 2d bitmap variable like this: Bitmap bmp[][] = new Bitmap[8][8] and tried to use a double for loop, but my app crashes because of
Boolean android.graphics.Bitmap.isRecycled() on a null reference
source 2 I tried alexander zak's answer but I am not sure how to draw squares on to the screen using the Bitmap return value.
Any one have any suggestions on how I can accomplish my goal? thanks for all your help.
Figured it out by:
Create a bitmap objectBitmap bmp = Bitmap.createBitmap(8, 8, bitmap.Config.ARGB_8888)
set each pixels(64) bmp.setPixel(index_of_the_bitmap_x, index_of_the_bitmap_y, int color)using a double for loop or which ever.
Draw a bitmap onto the canvas and scale it to the size of a rectangle: canvas.drawBitmap(bmp, null, destinationRet, null);
Related
My question is: a bitmap had to be square or is possible delete the invisible parts around the colored image? I have a bitmap in a SurfaceView with an hand in the center and i want calculate the bounds of this hand deleting the invisible around it, cause i have problems with the onClick Method. Without calculate every X and Y, is possible know the bounds of the hand with a Method or other things? Thanks in advance.
The best thing you could do would be to take the picture of the hand and crop it down to the size of the hand.
This way, you'll have a smaller file and won't have to implement some kind of code work around.
About your first question: any bitmap that retains some level of transparency has to have an alpha channel so in your case a hand has an alpha channel thus you cannot just delete those alpha pixels, because if you do the transparent part will remain black. You will have to use either ARGB_4444 format or ARGB_8888 format to retain this alpha channel. As far as getting the bitmaps bounds, use a Rect or some bounding shape maybe an oval, to accurately know if your finger is touching it, you can't just know exactly if your touch is within the bounds of this hand because this hand image, at certain portions contain different widths, and heights, however you can test if your touch is touching your hand exactly by using pixel perfect collision. Here is how it works:
class drawingView extends View {
Rect rect = new Rect();
Bitmap bitmap = yourHand;
#Override
public void onDraw(Canvas canvas) {
canvas.drawBitmap(bitmap, x, y, null);
rect.set(x, y, bitmap.getWidth(), bitmap.getHeight());
invalidate();
}
public void onClick() {
if(rect.contains(event.getX(), event.getY(), && bitmap.getPixel(event.getX() - rect.left, event.getY() - rect.top) != Color.TRANSPARENT) {
// you know you exactly touched the hand even out of the transparent region
}
}
I started you off I'm sure you will understand what's going on here.
Hope this helps :)
The idea is to use a png file with the right transparency in place, which means the part around the hand should have the transparency .
Now for retaining the transparency of the image when using the BitmapFactory,make sure that your image is pulled in as ARGB_8888
I'd like to create a square thumbnail of an image using Java. I've already managed to resize images through a couple of ways. However I'd like to create a real square image, also from a non-square image.
Example: the source has a size of 200x400 (widht/height)
the target size is 100x100
The algorithm would then need to resize the image to 50x100 and add 25x100 pixels of whitespace each on the left and on the right.
Can anyone help me with this?
Just create a 100x100 background; add the scaled image to it. Use Math.max(width, height) to determine the scale factor. Then, plot the scaled image over the background, use calculations (offset x, offset y) to put it in the proper position.
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.
For example: I have a bitmap 20x20, then I do some calculation and I need a new bitmap 30x30 with inside the old one for example in the center of the new bitmap. Is there a way to do that?
I'm creating an image dynamically using a canvas=canvas(mybitmap) but my image has no fixed dimensions. For example, I want to draw a path of a man into space. I create dynamically the image so that, if the man goesto east direction I will have a bitmap that grows in orizontal dimension (1x1 1x2 1x3....) andso on. if he turns to the north direction the bitmap must grow in vertical dimension (1x5 2x5 3x5).
I want not to redraw all the image but copy the old one into the new extended one and add only the new "data".
(I can't draw always on the same image because maybe I have to translate all the image and add some "space" in the first column or the first row of the bitmap")
Why not use a matrix to resize the bitmap?
If you can't just resize the one bitmap, why not still use a matrix then drawBitmap to your canvas(Bitmap) if you really need it done like that...
http://thinkandroid.wordpress.com/2009/12/25/resizing-a-bitmap/
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.