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.
Related
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);
v2 of the Google Maps APIs expose a snapshot method for obtaining Bitmap representations of GoogleMap objects as they appear in our UI.
This method is definitely required: for instance, if I try to manually create a Bitmap representation of my entire application UI as follows:
View view = activity.getWindow().getDecorView().getRootView();
Bitmap bitmap = Bitmap.createBitmap(
view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
view.draw(canvas);
the map is not captured (the corresponding pixels in bitmap are all black).
However, if I include a MapView in my Activity layout, and then inspect the view hierarchy at runtime using Stetho, it appears that at least part of the MapView (probably overlays) is rendered using an internal hierarchy of 'regular' Views, like RelativeLayout, LinearLayout, ImageView, etc.:
My question is: what's different about the MapView? Why does drawing to a Canvas not capture any part of the map at all (not even the 'regular' views used to render parts of it)?
For reference, the implementation I gave above can successfully capture SurfaceView contents, as well as all other 'regular' views.
If I had to guess, they are using setSecure() on the SurfaceView that is the map.
I've read pretty much every post on this topic, but none of them seem to help.
I'm trying to capture a screen shot of the current screen. For this I'm using getDrawingCache. Here's my code:
mRootView.get().setDrawingCacheEnabled(true);
mRootView.get().buildDrawingCache();
Bitmap screenShot = Bitmap.createBitmap(mRootView.get().getDrawingCache());
mRootView.get().setDrawingCacheEnabled(false);
The resulting bitmap is always black.
mRootView is a weak-reference to the drawer layout, which is my root view.
Here's what I've tried:
Adding a measure and layout call (although this shouldn't be needed since this code runs when a button is pressed, so the view should already be layed out).
Setting the layer type to LAYER_TYPE_NONE before calling setDrawingCacheEnabled(true)
Using a different view as the root view (for example, a ViewPager inside the DrawerLayout).
Nothing seems to work and I've run out of ideas.
You can draw your view to canvas-
pass your main layout reference to this method-
Bitmap file = save(layout);
Bitmap save(View v)
{
Bitmap b = Bitmap.createBitmap(v.getWidth(), v.getHeight(), Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b);
v.draw(c);
return b;
}
If above solution not working so just follow this link might be helpful convert view to bitmap
I am experimenting with building a custom game engine based on standard Canvas drawing that resembles the Android View class in that drawable objects have an onDraw method. The idea is that the game elements are given a Canvas on which to draw themselves and that is then incorporated into the main view. I assume that there is something similar going on behind the scenes with the standard Android Views.
How can I create a "master view" that can hand small Canvases to child objects and then incorporate those canvases into it's own drawing?
NOTE: the child objects are not subclasses of View, but the "master" view can be a View
After reading the Android Source Code for View and ViewGroup I discovered how this is done. When the system draws views to the screen, it generates a Canvas which is a rastor for the entire screen. That Canvas is not "cut up" and given to subviews to draw on, but rather it is translated and clipped to accomodate each view.
Before a particular view is drawn, its parent translates the Canvas to the child's position, clips the Canvas to the child's size, and then calls .draw(Canvas) on that view. The view does not know that the canvas has been altered, each view draws as if it were at (0,0). After the child has finished drawing, the Canvas is translated back to its previous position. Abstractly, every view thinks it is the only view, when in reality each parent is repositioning and resizing the canvas to be in the right place.
So for my situation, all I needed to do was iterate each of my child objects like this:
for(MyObject mob : mobjects) {
final int restoreTo = canvas.save();
canvas.translate(mob.getX(), mob.getY());
canvas.clipRect(0, 0, mob.getWidth(), mob.getHeight());
mob.onDraw(canvas);
canvas.restoreToCount(restoreTo);
}
I used to snap bitmaps of activities by taking their content view and drawing it:
View view = activity.findViewById(android.R.id.content)
Bitmap bitmap = Bitmap.createBitmap(
view.getWidth(), view.getHeight(), Config.ARGB_8888
);
view.draw(new Canvas(bitmap));
Now I'm using an ActionBar, and it's not nested under the content view, so it's left out. How can I obtain the real root view? Or snap a picture with the action bar in some other way, if that's not possible?
To get a Bitmap for the entire window including ActionBar you can use the DecorView.
First you need to enable drawing cache
getWindow().getDecorView().setDrawingCacheEnabled(true);
get the bitmap
Bitmap bmp = getWindow().getDecorView().getDrawingCache();
Use the bitmap elsewhere, I try this with a ImageView and works great.
disable drawing cache
getWindow().getDecorView().setDrawingCacheEnabled(false);