添加背景图像到黑莓应用程序无法正常工作

时间:2013-05-02 03:12:38

标签: blackberry blackberry-simulator

我是黑莓开发的新手,想要将图像添加到示例黑莓应用程序中。我尝试了多个教程,但图像没有在后台显示

任何人都可以告诉我这是什么问题吗?

/**
 * This class extends the UiApplication class, providing a graphical user
 * interface.
 */
public class Diverse extends UiApplication {

    private Bitmap backgroundBitmap;
    private Bitmap fieldBitmap;


    public static void main(String[] args) {

        Diverse diverse = new Diverse();
        diverse.enterEventDispatcher();
    }

    /**
     * Creates a new MyApp object
     */
    public Diverse() {
        //The background image.
        backgroundBitmap = Bitmap.getBitmapResource("background.png");

        MainScreen mainScreen = new MainScreen();

        HorizontalFieldManager horizontalFieldManager = new HorizontalFieldManager(HorizontalFieldManager.USE_ALL_WIDTH | HorizontalFieldManager.USE_ALL_HEIGHT){

            //Override the paint method to draw the background image.
            public void paint(Graphics graphics)
            {
                //Draw the background image and then call paint.
                graphics.drawBitmap(0, 0, 240, 240, backgroundBitmap, 0, 0);
                super.paint(graphics);
            }            

        };

        //The LabelField will show up through the transparent image.
        LabelField labelField = new LabelField("This is a label");

        //A bitmap field with a transparent image.
        //The background image will show up through the transparent BitMapField image.
        BitmapField bitmapField = new BitmapField(Bitmap.getBitmapResource("field.png"));

        //Add the manager to the screen.
        mainScreen.add(horizontalFieldManager);

        //Add the fields to the manager.
        horizontalFieldManager.add(labelField);
        horizontalFieldManager.add(bitmapField);

        // Push a screen onto the UI stack for rendering.
        pushScreen(new DiverseScreen());
    }
}

DiverseScreen类是

package diverse;

import net.rim.device.api.ui.container.MainScreen;

/**
 * A class extending the MainScreen class, which provides default standard
 * behavior for BlackBerry GUI applications.
 */
public final class DiverseScreen extends MainScreen
{
    /**
     * Creates a new MyScreen object
     */
    public DiverseScreen()
    {        
        // Set the displayed title of the screen       
        setTitle("Diverse");
    }
}

1 个答案:

答案 0 :(得分:1)

问题是您已为一个屏幕设置了背景图片,但您从未显示 那个屏幕。然后,您显示了一个不同的屏幕,设置了背景图像。

首先,它有助于理解BlackBerry UI框架。它允许您在屏幕上创建对象层次结构。在顶层,您有Screen(或Screen的子类),然后在Screen内,您有Managers,其中Fields }}。但是,必须将每个级别添加到某种容器中,最后,必须使用类似pushScreen()的内容显示顶级Screen

See a little more description on this hierarchy in a recent answer here

在您的情况下,您应该更改此行

    MainScreen mainScreen = new MainScreen();

    DiverseScreen mainScreen = new DiverseScreen();

然后更改此行

    pushScreen(new DiverseScreen());

    pushScreen(mainScreen);

因为mainScreen是您添加了绘制背景图片的水平字段管理器的实例。