BlackBerry - Background bitmap doesn't fit for scrolling page

纵然是瞬间 提交于 2019-12-11 13:24:07

问题


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



来源:https://stackoverflow.com/questions/1916523/blackberry-background-bitmap-doesnt-fit-for-scrolling-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!