This article describes how to store the contents of the phone screen as an image object, where it is thought that a canvas is displayed on the phone screen. The idea of completing this function is to use a buffering mechanism. We can't get the pixels on the canvas directly, so we can't get the image object directly from the content on canvas. Convert the idea, if you want to draw the contents of the canvas first to a image, and this image does not appear on the screen, but only after the painting is completed one-time display to the screen. Experienced friends must have a double buffering mechanism, but here is not to use double buffering to solve the problem of the splash screen, but to get the current canvas content.
Let's write a simple canvas class to test the idea that Simplecanvas is a subclass of canvas, in order to preserve canvas content, we create an image that is the same size as the canvas.
Class Simplecanvas extends canvas{
int W;
int h;
Private Image offimage = null;
Private Boolean buffered = true;
Public Simplecanvas (Boolean _buffered) {
buffered = _buffered;
W = getwidth ();
h = getheight ();
if (buffered)
Offimage = Image.createimage (w,h);
}
protected void Paint (Graphics g) {
int color = G.getcolor ();
G.setcolor (0xFFFFFF);
G.fillrect (0,0,W,H);
G.setcolor (color);
Graphics save = g;
if (offimage!= null)
g = Offimage.getgraphics ();
Draw the Offimage
G.setcolor (128,128,0);
G.fillroundrect ((w-100)/2, (h-60)/2,100,60,5,3);
Draw the offimage to the canvas
Save.drawimage (offimage,0,0,graphics.top| Graphics.left);
}
Public Image Printme () {
return offimage;
}