Android Moving Bitmap in canvas - android

I have an activity with an imageView which has a canvas, and under it a button. when I click the button a new bitmap gets drawn into the canvas with text ..
How do I make the drawn bitmap moveable like I drag it into any place I want in the canvas ?
Canvas c = new Canvas(WorkBitmap);
c.drawText("Bitmap", 360, 520, null);
c.drawBitmap(ball, x, y, null);
I saw some tutorials on SurfaceView and I did them, thye worked fine. But I want the bitmap in my canvas to move, in the tutorials its the whole screen a surfaceView canvas ..

You need to use the SurfaceView widget that is available in the XML
But if you want the image to be move-able around all the screen you could use SurfaceView
Check this Youtube video, It's very informative (watch all the SurfaceView playlist to benefit)

Related

Use an image as the background of a canvas and keep it resized

I'm currently working on an Android app. I have a canvas onto which the user will draw something. I need to have a picture as the background of this canvas.
I already managed to import a pic and set it as the background using
mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.my_background_pic));
However, the picture appears ten times bigger than the screen.
Is it possible to keep the picture resized, so it fills the canvas and not more?
Also, later I will need to add other things to the activity. The canvas will not be the only thing on the screen, so I need the picture to be the size of the canvas, and not the screen.
Thanks a lot.
Nevermind I found it:
canvas.drawBitmap(bitmap, null, new RectF(0, 0, canvasWidth, canvasHeight), null);
By Kai at stackoverflow.com/a/27466127/8205282

Save image on Canvas to bitmap

How can I save the image on a canvas to bitmap?
There are several related posts, but I didn't find the one works for me. To make it clear, here is my problem
What's on my canvas:
An bitmap image which was drawn to the canvas using canvas.drawBitmap(oldBitmap, x, y, null);
Some paths drawn overlay to this image on the same canvas using canvas.drawPath.
Both the image and the paths are draw on a customized SurfaceView. And both they are draw to the canvas in the onDraw function. Or more specific, the implementation is in the following link:
Android: custom draw on an given image (with SurfaceView)
Now I need to save the image on canvas (the oldBitmap and paths I drew) to a newBitmap. Could someone tell me how to do that?

How to cache surfaceview in a bitmap?

In my android app there are two SurfaceViews. One SurfaceView is drawn upon when user touches it. I want to copy and replicate the content of this SurfaceView on the other SurfaceView. Probably this can be done by caching the content of the first SurfaceView in a bitmap and then drawing the bitmap on the second SurfaceView. But how to cache the first SurfaceView ?
There are a few similar questions on this forum but they really do not work out for me.
This: surfaceview.getDrawingCache(); will return the bitmap out of the SurfaceView and then you can draw it to the second SurfaceView: canvas.drawBitmap(bitmap, 0, 0, null);.
Another way is to copy the canvas, not the surface view, like this:
Bitmap bitmap = Bitmap.createBitmap(WIDTH, HEIGHT, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
This means, that if you'll draw something on the canvas, it will be actually drawn on the bitmap.

drawBitmap() of canvas gives white image on parent image android

I'm newbie with canvas.
I'm trying to draw child bitmap on parent bitmap using canvas.drawbitmap(childbitmap,matrix,point) method. I'm getting number of bitmaps using loop and trying to overlay image on all that bitmaps But somehow the output which I got haven't original child image. It looks like white image. so I'm able to seen child image on parent one but don't know why it looks like this?
Let me put my code over here with output image.
Canvas mCanvas = new Canvas(bitmap);
Paint p = new Paint();
Bitmap icon = BitmapFactory.decodeResource(getApplicationContext().getResources(),
R.drawable.ic_launcher);
mCanvas.drawBitmap(icon,0,0,p);
This above code will run with loop and it will give multiple "bitmap" object on which I have to draw canvas. So now I'm taking launcher icon as child bitmap.while checking final output it shows like below image :
Check the white image instead of launcher icon.So what's wrong with my code?
Waiting for your best suggestion ASAP.
You are drawing your bitmap on top with the current canvas. So you have to call mCanvas.drawBitmap a second time to draw the foreground bitmap.

Android canvas rotate calculation

I'm creating a live wallpaper that I want a Picture to cover the entire canvas so I draw it like this when in landscape mode
Rectangle r = new Rect(0, canvas.getHeight()- (canvas.getHeight()-statusBarHeight), canvas.getWidth(), canvas.getHeight());
c.drawPicture(pictureBackground, r);
Notice I have to take into consideration the status bar. Okay, that works fine. Now what I want to do is rotate the canvas 90 degrees for portrait mode (btw I can't just rotate the picture outside of code, have to do it this way) so I do this:
canvas.rotate(90,canvas.getWidth()/2,canvas.getHeight()/2);
Now what rectangle should I use to cover the canvas in its entirety when I go to draw the picture again (remember I have to account for the status bar)????
Rectangle r = new Rect(?,?,?,?)
I can't figure it out, I've tried so many possible combinations

Categories

Resources