Android - Draw Picture over ImageView - java

I have pretty complex custom ImageView and on the other hand I have Picture object that I want to draw it on the ImageView's canvas? Is this possible? The Picture object is not to be converted in any other type since then it'll lose quality and staff, that's why I go with Picture..
Thanks

You have at least 2 options:
1 - Retriev bitmap from ImageView. Create canvas from it:
Canvas canvas = new Canvas(mImage);
Draw on that canvas.
2 - Put transparent custom View/SurfaceView over ImageView and draw on it.

Related

How to get Bitmap from another Bitmap and color?

I am writing a game program for educational purposes. I'm using the SurfaceView class. The program displays the images on the screen using the Canvas object. The drawable folder contains a png-image. By the name of the picture "bitmapName" I get the Bitmap using this code
int resID = context.getResources().getIdentifier(bitmapName, "drawable", context.getPackageName());
Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), resID);
I use the drawBitmap() method to draw the resulting Bitmap to the screen. The problem is that the object on which this picture is superimposed is used in the program quite often and the picture should have different shades depending on the logic. The image is now yellow, then green, and other shades.
And there are a LOT of shades. The easiest way to do this is simply to create images of different shades in advance and load them into memory. But uploading a lot of pictures will greatly increase the size of the application, and I would not want this. I tried this in Photoshop. I put a layer on top of the image and filled it with a solid color. Lowered the opacity of this layer. The result is a picture that looks like what I want. Therefore, the question is: how to create a Bitmap from another ready-made Bitmap and some color with transparency.
I need a method to which I send Bitmap, color, transparency level; and get a ready-made Bitmap of a changed shade from it. Can you tell me how can I achieve this result? How to overlay color on Bitmap?

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!

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.

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

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.

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