views:

272

answers:

2

Hello all,

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

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

}

+2  A: 

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
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));
        }
    }
}
Max Gontar
When i added the first set of code and compile it, it throws error as 'cannot find symbol getMainManager()'. I imported import net.rim.device.api.ui.Manager;, but still observing the same error. Am i doing anything wrong?
I also have import net.rim.device.api.ui.container.MainScreen; but still the same error.
Please check updated code.
Max Gontar
I have gone through the code to understand and then tested, it works like Charm! Thank you so much, really appreciate your timed help!
You're welcome!
Max Gontar
Hi Max,I found an issue and facing one problem with your code usage.As i already mentioned,i added your code to apply background bitmap in the background of my app screens without any issues.But i see now, by default it is creating two pages(we can scroll to second page though there is no controls there) instead of one page initially.As i need to have scroll bar for my app screens, i added super(VERTICAL_SCROLL | VERTICAL_SCROLLBAR);. But i have controls only in one page, if user enters something, then it will need scroll.But this code creates second page also by defualt when 2nd page is none.
Hi there! Can you update your question with code you mention?
Max Gontar
Actually i keep the same your code(Answer 1) in a java file and then call that from my other files like below:background = Bitmap.getBitmapResource ("HomeBack.png"); add(_container = new BGVerticalFieldManager(background));--- Adding other controls like LabelField, EditField etc here ---Whatever the controls that i have are only in first page, will not need scroll to go to second page.But if user enters data, it may need scroll bar,so i keep scroll bar for those screens. But after calling class to draw bitmap background of a screen,i could see it is creating two pages.
A: 

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

1in9ui5t