drawBitmap() of canvas gives white image on parent image android - 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.

Related

Saving Multiple Circles to Bitmap android

I need to save a number of formatted circles to a Bitmap whenever a button is pushed, and then draw those circles to the canvas. I was looking around, but had a great deal of trouble finding resources on how to save a shape I had created that was not an already created resource file. Any advice on how to go about this would be appreciated.
Say you are in your custom view, but it should work also with a OnTouchListener in any view.
Create a mutable bitmap with Bitmap.createBitmap() with the same width and height of your view
use your logic for getting information of the circles, for example you can use onTouchEvent() to get the coordinates of you finger(s)
with this information you can use Bitmap.setPixel() to save it to the bitmap
optional, onDraw() you can draw your bitmap with Canvas.drawBitmap()
In case you need to draw shapes to the bitmap you can create a new Canvas(bitmap) and draw on this canvas: the pixels will be set on the bitmap.

Android - Extend a Bitmap to use as MapMarker

I want to use downloaded images as markers on a MapView. I images are all squares but I would like the bottom to also extend to form a triangle marking the exact point.
My approach was to create a canvas which is slightly larger than the image. Then, draw the bitmap on to the canvas and then somehow pick the color from the bottom of the bitmap and draw a triangular shape from the horizontal center of the image to the bottom of the canvas. Something like this...
As you might guess I'm stuck with the last part. So far I have....
Bitmap canvasBitmap = Bitmap.createBitmap(markerBitmap.getWidth(), markerBitmap.getHeight()+10, Bitmap.Config.ARGB_8888);
// The 10 pixels will be the so called "pin"
Canvas canvas = new Canvas(canvasBitmap);
canvas.drawBitmap(markerBitmap, 0.0f, 0.0f, null);
// Can figure out how to draw the 10 px bottom using the color from markerBitmap
Help !!
Is there any specific reason why you want to create the images dynamically? If not, why don't you create them using a graphics editing program, save them as png's or 9-patch images and then simply assign it to the Drawable that's used for the marker.

How to create an image by giving the specifications

I want to create an image (e.g coupons) in my applications by giving the specifications in the edit text.Let suppose i have 3 edit texts for name,color,content respectively.when i fill these details and click on submit button, A image should generate containing all the specifications that i have giving in the edit text so that user can change it any time and generate the desired image.
how can i achieve this?
You can create a Bitmap of whatever size you want for your coupons:
Bitmap coupon = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Then you create a Canvas for drawing into the Bitmap:
Canvas canvas = new Canvas(coupon);
Draw your coupon using canvas with the same calls as you would when drawing to the screen. Finally, you can display the Bitmap on the screen either in a custom view's onDraw method or by creating an ImageView and calling it's setImageBitmap method with your coupon bitmap.

Android: how to simply draw a bitmap on another bitmap

I'm new to Android dev and I'm having a hard time trying to do something which seems obvious to me: drawing little images on top of a bigger image.
Let's say that I have a 500x500 image and I want to draw icons at different locations. Icons are png files that I load with:
Bitmap img =
BitmapFactory.decodeResource(getResources(),
R.drawable.idIcon1)
My "background image" is a LayerDrawable.
Then, I am totally lost... Do I have to create a canvas ? How to draw on my "background image" my icons at different positions?
int positionLeft=0;
int positionTop=0;
Bitmap newBitmap =Bitmap.createBitmap(backgroundBitmap.getWidth(),bitmap.getHeight(),Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(newBitmap);
canvas.drawBitmap(backgroundBitmap, positionLeft, positionTop,null);
positionLeft=100;
positionTop=100;
canvas.drawBitmap(iconBitmap,positionLeft,positionTop,null);
imageView.setImageBitmap(newBitmap);
You're making simple things difficult. Just use a layout with android:background attribute, and then add ImageViews dynamically with the necessary bitmaps inside.

Tiling a Bitmap on a Canvas

I would like to create a 'graph paper' look to the Bitmap I am drawing via a Canvas, and trying to figure out the best way to do this.
I can't pass a source Bitmap containing the graph paper background to the Canvas constructor, as I am getting the Canvas in a SurfaceView via the .lockCanvas() call.
Some solutions I've tried:
I've tried implementing this solution in my SurfaceView's Thread.run(), but the issue I believe is when the BitmapDrawable is converted to a Bitmap... it loses the tiling properties.
canvas = mSurfaceHolder.lockCanvas(null);
BitmapDrawable TileMe = new BitmapDrawable(BitmapFactory.decodeResource(getResources(), R.drawable.editor_graph));
TileMe.setTileModeX(Shader.TileMode.REPEAT);
TileMe.setTileModeY(Shader.TileMode.REPEAT);
Bitmap b = TileMe.getBitmap();
canvas.drawBitmap(b, 0, 0, null);
If I use the Canvas.drawBitmap method that takes a destination RectF as a parameter, it looks like the bitmap will be tiled to fill the RectF... but how do I declare a RectF reliably that fills the entire view area?
Setting the Activities background to the desired graph paper look also doesn't work, as the bitmap/canvas layout is opaque and blocks that from being seen.
Any ideas how to achieve this?
You have two easy solutions:
Either use a BitmapDrawable, but instead of extracting the Bitmap, just call BitmapDrawable.draw(Canvas). Don't forget to set the drawable's bounds to fill your drawing area.
Create a Paint with a BitmapShader and draw a rectangle with it (this is basically what BitmapDrawable does).
I'm sure there is a way to get a tiled effect using a SurfaceView. Unfortunately, it looks like you can't use the BitmapDrawable with a canvas. So you would probably have to implement you own custom tiling method by creating your own series of Rect's on the Canvas and drawing a scaled bitmap to each one.
It honestly wouldn't be that hard. Just get the width/height of the view, and create an array of Rect's based on this data that you will draw the Bitmap to.
Alternatively, if you don't need to make modifications to the actual tiled background on the fly, just draw it as a background and draw the SurfaceView on top of it. That post you linked provided multiple solutions to tiling a BitmapDrawable that you could implement.

Categories

Resources