2

我想获取显示在 BrowserField 中的网页内容的位图。因此我需要浏览器字段的图形对象。但不幸的是,paint-method 受到了保护。有没有办法得到这个?

谢谢

4

1 回答 1

0

通常,如果您想使用字段进行一些自定义绘图,即。绘制到字段的图形上下文中,您将继承 Field 并覆盖paint方法。但是,当涉及到 BrowserField 时,您不能这样做,因为它被声明为final

不过,有一个解决方法。您可以将 Manager 子类化并将 BrowserField 添加到该管理器的实例中。因此,例如,如果您想将 BrowserField 实例添加到 VerticalFieldManager,您可以使用以下代码来访问将要绘制浏览器的 Graphics 对象。在这个示例代码中,您将看到我使用图形对象和管理器的超类实现来绘制位图。然后,该位图被绘制到屏幕上。

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);

    }
};

而且,您有一个包含管理器内容的位图。但是应该注意,这可能会消耗大量内存。

于 2011-09-25T04:13:22.280 回答