【IT168 技术】本文介绍如何将手机屏幕的内容存储为Image对象,这里认为手机屏幕上显示的是一个Canvas。完成这一个功能的思想就是使用缓冲机制。我们不能直接获得Canvas上的像素,因此不能直接从Canvas上的内容获得Image对象。转换一下思路,如果把要绘制的Canvas上的内容首先绘制到一个Image上,而这个Image并不显示到屏幕上,只是在绘画完成后一次性的显示到屏幕上。有经验的朋友一定联想到了双缓冲机制,不过这里并不是要使用双缓冲解决闪屏的问题,而是要得到当前Canvas的内容。
下面我们编写一个简单的Canvas类来测试一下这个想法,SimpleCanvas是Canvas的子类,为了保存Canvas的内容,我们创建一个Image,大小与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;
}
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;
}
可以看到paint()方法,并不是直接对Canvas操作,而是先把要画的内容绘制到一个Image上,然后再绘制到Canvas上。这样到你想抓取屏幕内容的时候就可以调用printMe()方法了,返回offImage。编写一个MIDlet测试一下这个效果。
package com.J2MEdev;
import Javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
/**
*
* @author mingjava
* @version
*/
public class PrintScreen extends MIDlet implements CommandListener{
private Display display = null;
private SimpleCanvas canvas = new SimpleCanvas(true);
private Command printCommand = new Command("Print",Command.OK,1);
public void startApp() {
if(display == null)
display = Display.getDisplay(this);
canvas.addCommand(printCommand);
canvas.setCommandListener(this);
display.setCurrent(canvas);
}
public void pauseApp() {}
public void destroyApp(boolean unconditional) {}
public void commandAction(Command command, Displayable displayable) {
if(command == printCommand){
Form form = new Form("screen");
form.append(canvas.printMe());
display.setCurrent(form);
}
}
}
import Javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
/**
*
* @author mingjava
* @version
*/
public class PrintScreen extends MIDlet implements CommandListener{
private Display display = null;
private SimpleCanvas canvas = new SimpleCanvas(true);
private Command printCommand = new Command("Print",Command.OK,1);
public void startApp() {
if(display == null)
display = Display.getDisplay(this);
canvas.addCommand(printCommand);
canvas.setCommandListener(this);
display.setCurrent(canvas);
}
public void pauseApp() {}
public void destroyApp(boolean unconditional) {}
public void commandAction(Command command, Displayable displayable) {
if(command == printCommand){
Form form = new Form("screen");
form.append(canvas.printMe());
display.setCurrent(form);
}
}
}
运行PrintScreen,选择Print,即可把当前的屏幕显示到一个Form中。如下图所示: