As a reader asked for it, I provide a tutorial on how to generate and save a simple screenshot from a SurfaceView.
This tutorial is based on the 2D Tutorial Series – Part V and the tutorial How to create an option menu. If you have no idea about how a SurfaceView works, please start the 2D Tutorial Series.
Lets start by getting the code from the 2D Tutorial.
We start by removing the animation from the code. That means strip the class Element to the bare bitmap and the used coordinates.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | public class Element { private float mX; private float mY; private Bitmap mBitmap; public Element(Resources res, int x, int y) { mBitmap = BitmapFactory.decodeStream(new BufferedInputStream(res.openRawResource(R.drawable.icon))); mX = x - mBitmap.getWidth() / 2; mY = y - mBitmap.getHeight() / 2; } public void doDraw(Canvas canvas) { canvas.drawBitmap(mBitmap, mX, mY, null); } } |