BlackBerry - Background bitmap doesn't fit for

2019-08-08 16:28发布

问题:

I have a background bitmap in my Blackberry application screen. Screen has scrolling enabled as i must to have a scroll. The problem which i'm facing is, when i scroll down the page, background bitmap doesn't fit for the scrolled page, rather it shows just plain white background. Do we need to draw the background bitmap for every scrolling page?

My bitmap size is: 360 * 480

Updated code is:

class BGVerticalFieldManager extends VerticalFieldManager {
    Bitmap mBgBitmap = null;
    int mBgWidth = -1;
    int mBgHeight = -1;
    int mBgX = -1;
    int mBgY = -1;

    public BGVerticalFieldManager(Bitmap background) {
            super(USE_ALL_WIDTH | USE_ALL_HEIGHT | VERTICAL_SCROLL
                            | VERTICAL_SCROLLBAR);
            mBgBitmap = background;
            mBgWidth = mBgBitmap.getWidth();
            mBgHeight = mBgBitmap.getHeight();
            mBgX = (Display.getWidth() - mBgWidth) >> 1;
            mBgY = (Display.getHeight() - mBgHeight) >> 1;

    }

    protected void paintBackground(Graphics graphics) {
            paintBackgroundBitmap(graphics);
            invalidate();
    }

    /*private void paintBackgroundBitmap(Graphics graphics) {
            if (null != mBgBitmap) {
                    int x = mBgX + ((MainScreen)getScreen())
                        .getMainManager().getHorizontalScroll();
                    int y = mBgY + ((MainScreen)getScreen())
                        .getMainManager().getVerticalScroll();

                    graphics.drawBitmap(x, y, mBgWidth, 
                        mBgHeight, mBgBitmap, 0, 0);
            }
    } */
    private void paintBackgroundBitmap(Graphics graphics) {
    if (null != mBgBitmap) {
        int x = mBgX
                + getHorizontalScroll();
        int y = mBgY
                + getVerticalScroll();
        graphics.drawBitmap(x, y, mBgWidth, mBgHeight, mBgBitmap, 0, 0);
    }
}

}

CALLING THE ABOVE BACKGROUND BITMAP CODE FROM THE ANOTHER FILE AS BELOW :

public MyFirstScreen ( String label, int screenState, int selectedObj, boolean bUI ) 
{    

   super(VERTICAL_SCROLL | VERTICAL_SCROLLBAR); // I must need it ...

   appTitle = label;
   setTitle(appTitle);

   background = Bitmap.getBitmapResource ("HomeBack.png");        
   add(_container = new BGVerticalFieldManager(background));

   ..............................
   ..............................
   ..............................

}

回答1:

To get actual scroll position you can use getVerticalScroll():

class BGVerticalFieldManager extends VerticalFieldManager {
    Bitmap mBgBitmap = null;
    int mBgWidth = -1;
    int mBgHeight = -1;
    int mBgX = -1;
    int mBgY = -1;

    public BGVerticalFieldManager(Bitmap background) {
        super(USE_ALL_WIDTH | USE_ALL_HEIGHT | VERTICAL_SCROLL
                | VERTICAL_SCROLLBAR);
        mBgBitmap = background;
        mBgWidth = mBgBitmap.getWidth();
        mBgHeight = mBgBitmap.getHeight();
        mBgX = (Display.getWidth() - mBgWidth) >> 1;
        mBgY = (Display.getHeight() - mBgHeight) >> 1;

    }

    protected void paintBackground(Graphics graphics) {
        paintBackgroundBitmap(graphics);
        invalidate();
    }

    private void paintBackgroundBitmap(Graphics graphics) {
        if (null != mBgBitmap) {
            int x = mBgX
                    + getHorizontalScroll();
            int y = mBgY
                    + getVerticalScroll();
            graphics.drawBitmap(x, y, mBgWidth, mBgHeight, mBgBitmap, 0, 0);
        }
    }
}

alt text http://img693.imageshack.us/img693/6245/9000.jpg
Sample of use:

class Scr extends MainScreen {

    private BGVerticalFieldManager mContainer;

    public Scr() {
        super(NO_VERTICAL_SCROLL);
        setTitle("Screen Title");
        Bitmap bitmap = Bitmap.getBitmapResource("BoldOEM.jpg");
        add(mContainer = new BGVerticalFieldManager(bitmap));
        for (int i = 0; i < 100; i++) {
            mContainer.add(new LabelField("List item #" + String.valueOf(i)));
            mContainer.add(new NullField(FOCUSABLE));
        }
    }
}


回答2:

Max Gontar's response is a battery killer;

protected void paintBackground(Graphics graphics) {
  paintBackgroundBitmap(graphics);
  invalidate();
}

As invalidate() will result in a call to paintBackground(Graphics).