我想得到网页内容的位图,因为它显示在BrowserField中。因此我需要浏览器字段的图形对象。但不幸的是,油漆方法受到了保护。
有办法弄到这个吗?
谢谢
最佳答案
通常,如果你想用一个字段来做一些自定义的绘图,比如在字段的图形上下文中绘图,你需要对字段进行子类化并重写绘制方法。但是,当谈到BrowserField时,您不能这样做,因为它已声明为final。
不过,有一个解决办法。您可以将管理器子类化,并将BrowserField添加到该管理器的实例中。因此,例如,如果要将BrowserField实例添加到VerticalFieldManager,可以使用以下代码访问浏览器将要绘制到的图形对象。在这个示例代码中,您将看到我使用graphics对象和manager的超类实现来绘制位图。然后,位图被绘制到屏幕上。
VerticalFieldManager vfm = new VerticalFieldManager() {
// Override to gain access to Field's drawing surface
//
protected void paint(Graphics graphics) {
// Create a bitmap to draw into
//
Bitmap b = new Bitmap(vfm.getVirtualWidth(), vfm.getVirtualHeight());
// Create a graphics context to draw into the bitmap
//
Graphics g = Graphics.create(b);
// Give this graphics context to the superclass implementation
// so it will draw into the bitmap instead of the screen
//
super.paint(g);
// Now, draw the bitmap
//
graphics.drawBitmap(0,
0,
vfm.getVirtualWidth(),
vfm.getVirtualHeight(),
b,
0,
0);
}
};
而且,这里有一个包含管理器内容的位图。不过,需要注意的是,这可能会消耗大量内存。