How to get a snapshot of the view? - android

I am currently using a View to trace the users fingers and draw lines depending on the color picked. I have set the background image using setBackGround() but I would now like to get the background + what the user user drawn into a variable. There exists getBackGround and getForeGround, not not both. How do I achieve this? I do not want to store the image locally on the persons phone.
The code used to achieve the tracing was used from this example and the setBackground was achieved by,
dv = new DrawingView(this);
dv.setBackground(imageview.getDrawable());
Where the imageview was defined and has data in it before.

You have to pass view(name of the view), Where a view is like a relativeLayout
view.setDrawingCacheEnabled(true);
view.buildDrawingCache();
Bitmap returnedBitmap = view.getDrawingCache();
imageView.setImageBitmap(returnedBitmap);

Related

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.

getDrawingCache() returns null

I'm working on a simple painting app and trying to implement the functionality of giving more space to draw when a user requests, which I thought could be done by simply enabling scrolling of my CustomView class(which is contained in a LinearLayout then in a ScrollView class). If I didn't resize the CustomView class by running Chunk 1, Chunk 2 works fine(It simply saves one screen of drawing. At this time, there is no scrolling.) A bummer! Chunk 2 fails(view.getDrawingCache() returns null) when Chunk 1 is run (At this time, scrolling is enabled). I want to save the entire view including the part left out due to scrolling.
Chunk 1:
CustomView view = (CustomView) findViewById(R.id.customViewId);
height = 2 * height;
view.setLayoutParams(new LinearLayout.LayoutParams(width, height));
Chunk 2:
CustomView view = (CustomView) findViewById(R.id.customViewId);
view.setDrawingCacheEnabled(true);
Bitmap bitmap = Bitmap.createBitmap(view.getDrawingCache());
view.setDrawingCacheEnabled(false);
These two chunks of codes are independent and small parts in two different methods.
Edited: SOLVED
After billions of Google queries, the problem is now SOLVED ;)
I referred to this thread, https://groups.google.com/forum/?fromgroups=#!topic/android-developers/VQM5WmxPilM.
What I didn't understand was getDrawingCache() method has the limit on the View class's size(width and height). If the View class is too big, getDrawingCache() simply returns null.
So, the solution was not to use the method and instead do it like below.
CustomView view = (CustomView) findViewById(R.id.customViewId);
Bitmap bitmap = Bitmap.createBitmap(view.getMeasuredWidth(),
view.getMeasuredHeight(), Bitmap.Config.ARGB_8888);
Canvas bitmapHolder = new Canvas(bitmap);
view.draw(bitmapHolder);
// bitmap now contains the data we need! Do whatever with it!
You need to call buildDrawingCache(), before being able to use the bitmap.
The setDrawingCache(true) thing just sets the flag and waits for the next drawing pass in order to create the cache bitmap.
Also don't forget to call destroyDrawingCache() when you no longer need it.

Adding a canvas to a relative layout

Currently I am writing a program that is supposed to add an image to a canvas and then add that canvas to a relative layout. The problem that I am having is that when I call this the layout is displayed but the canvas is not drawn on the layout.
canvas = (RelativeLayout) findViewById(R.id.canvasLayout);
imageCanvas = new Canvas();
mainImage = BitmapFactory.decodeResource(getResources(), R.drawable.image);
imageCanvas.drawBitmap(mainImage, 50, 50, null);
imageCanvas.drawColor(Color.BLACK);
canvas.draw(imageCanvas);
That is the code that is attempting to add the image to the canvas and then the canvas to the layout.
I am completely lost as to what to do to attempt to fix this.
The canvas.draw(imageCanvas) call you are using actually draws canvas (the RelativeLayout) onto imageCanvas (the Canvas object) - likely not what you were intending. There are several ways I would suggest to go about getting the drawable onto the RelativeLayout:
The first would be to create an ImageView, set it's image to your drawable, then add it to the layout. Depending on if you want the images to be added dynamically during runtime or not, you could actually do this in the xml graphic designer. This is probably the best way to go, as it gives you control over how large the image is, and how it gets scaled/cropped.
If you want to set a background to the layout, I believe you can actually set the Background attribute of the RelativeLayout directly to a drawable - this does not allow as much control over the scaling of the image though.
Finally, for complete control over how the RelativeLayout is draw, you can override the OnDraw method in any view. This method is called by the android system whenever it needs to refresh the view. If you do end up using this, creating a custom view class, which you then place in the RelativeLayout will probably work better.
canvas = (RelativeLayout) findViewById(R.id.canvasLayout);
imageCanvas = new Canvas();
mainImage = BitmapFactory.decodeResource(getResources(), R.drawable.image);
imageCanvas.drawBitmap(mainImage, 50, 50, null);
canvas.addView(imageCanvas);
The following will programmatically add an image to your layout from a bitmap resource which sounds like what you want to do. If you need to draw on top of that image, say, you would need to make a custom component by extending ImageView and overriding onDraw().
canvas = (RelativeLayout) findViewById(R.id.canvasLayout);
imageView = new ImageView(getApplicationContext());
mainImage = BitmapFactory.decodeResource(getResources(), R.drawable.image);
imageView.setImageBitmap( mainImage );
canvas.addView( imageView );

flipboard in Android

I want to use clipboard effect on android. I've seen some source codes about it. But all of them are simply flipping over some static pictures(Bitmap).I want to flip a view, and the view changes dynamically. How can I flip over this sort of view? I've tried to take a snapshot of the view, then use the snapshot to flip. But it seems that I can only snapshot a view after it presents on screen. How can I get the Bitmap of a view without present the view on screen?
One way is to draw your view hierarchy onto a bitmap and then use that for the animation. This blog post contains a few simple instructions. Basically it boils down to:
Bitmap returnedBitmap = Bitmap.createBitmap(screenWidth, screenHeight,Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(auxBitmap);
myview.draw(canvas);
Where myview is the view hierarchy you wish to draw (either created in code or an inflated layout) and returnedBitmap is your bitmap with the view on it.

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.

Categories

Resources