Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Developer
peter_strange
Posts: 19,608
Registered: ‎07-14-2008
My Device: Not Specified
Accepted Solution

Struggling with centered background Image

[ Edited ]

I have code that I thought would always give me a centered background image on a MainScreen.  It is based on the ideas presented in here:

 

http://supportforums.blackberry.com/t5/Java-Development/MainScreen-explained/ta-p/606644

 

However, the code does not work, at least not for me.

 

I've tried other variations too, like adding another non scrolling VFM and then adding the scrolling data to that VFM, but even that indirection suffers the same problem.  Have two screen shots to demonstrate, the 'exclamation' should be in the center according to me!

 

Code follows.

 

To demonstrate the problem, just space out the Edit Field with 'enters' to more than fill the screen.  Then do the same to the one below the Bitmap  Then scroll up and down using the trackpad.  I suspect you will see the problem.

 

I've seen this on OS 6.0 and OS 7.0 Simulators.  Not tested on OS 5.0 and only tested on touchscreen devices.  I will do some more testing later. 

 

Anyone got any suggestions regarding how we get the 'centered' background Bitmap to actually stay in the center will still letting me add lines to my editfields and scroll using the trackpad?.

 

import net.rim.device.api.system.*;
import net.rim.device.api.ui.*;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.ui.container.*;

/**
 * A MainScreen class to test background Bitmaps.
 */
public class BackgroundImageScreen extends MainScreen
{    
    private int backgroundColor = 0x00FF0000;
    private Bitmap _backgroundBitmap = Bitmap.getPredefinedBitmap(Bitmap.EXCLAMATION);
   
    // Constructor
    public BackgroundImageScreen()
    {         
        super(Manager.USE_ALL_WIDTH | Manager.USE_ALL_HEIGHT | Manager.NO_VERTICAL_SCROLL | Manager.NO_VERTICAL_SCROLLBAR);
        setTitle(new LabelField("Background Image Demo"));          
        VerticalFieldManager backgoundManager = new VerticalFieldManager(Manager.USE_ALL_WIDTH | Manager.USE_ALL_HEIGHT | Manager.VERTICAL_SCROLL | Manager.VERTICAL_SCROLLBAR ) {
            public void paintBackground(Graphics g) {
                int currentVackgroundColor = g.getBackgroundColor();
                try {
                    g.setBackgroundColor(backgroundColor);
                    g.clear();
                } finally {
                    g.setBackgroundColor(currentVackgroundColor);
                }
                int managerWidth = this.getWidth();
                int managerHeight = this.getHeight();
                int centerX = (this.getWidth() - _backgroundBitmap.getWidth()) /2;
                int centerY = (managerHeight - _backgroundBitmap.getHeight()) /2;
                System.out.println("Current positioning: " + Integer.toString(centerX) + ":" + Integer.toString(centerY) +
                                    ", Manager extent: " + Integer.toString(managerWidth) + ":" + Integer.toString(managerHeight) +
                                    ", Manager virtual: " + Integer.toString(this.getVirtualWidth()) + ":" + Integer.toString(this.getVirtualHeight()) +
                                    ", ManagerScroll: " + Integer.toString(this.getHorizontalScroll()) + ":" + Integer.toString(this.getVerticalScroll()));
                g.drawBitmap(this.getHorizontalScroll() + centerX, this.getVerticalScroll() + centerY, _backgroundBitmap.getWidth(), _backgroundBitmap.getHeight(), _backgroundBitmap, 0, 0);
                   
            }
        };
        backgoundManager.add(new EditField("Scroll test: ", "Enter text in here to make it scroll"));
        backgoundManager.add(new SeparatorField());
        backgoundManager.add(new BitmapField(Bitmap.getPredefinedBitmap(Bitmap.QUESTION)));
        backgoundManager.add(new SeparatorField());
        backgoundManager.add(new EditField("Another Scroll test: ", "Enter text in here to make it scroll"));
        this.add(backgoundManager);

    }
}

Please use plain text.
Developer
shnuffy
Posts: 268
Registered: ‎08-19-2009
My Device: Bold 9700

Re: Struggling with centered background Image

When peter_strange has a problem, you know you're in trouble.

Please use plain text.
Developer
arkadyz
Posts: 2,268
Registered: ‎07-08-2009
My Device: various

Re: Struggling with centered background Image

Ahh, touch screens...

 

I remember that I had to invalidate the manager painting additional stuff (vertical scrollbar in my case) in scrollChanged() specifically for touchscreen devices. So one thing to try is to to implement ScrollChangeListener in your main screen or your added VFM and invalidate the VFM in scrollChanged. This should fix the problem even though it will be a performance hit.

 

I suspect that touchscreen devices have some graphical hardware acceleration making them more efficient when scrolling but missing the stuff of which BlackBerry framework is not aware (such as your paintBackground depending on the scroll positions).

 

I'm curious what happens if you use setBackground on a non-scrolling Manager and add the scrolling one with no background painting on top of it. setBackground, being a built-in feature, might make better use of their hardware acceleration.

----------------------------------------------------------
please click 'Accept Solution' on posts that provide the solution to the question you've posted. Don't say "Thanks", press 'Like' button instead!
Please use plain text.
Developer
peter_strange
Posts: 19,608
Registered: ‎07-14-2008
My Device: Not Specified

Re: Struggling with centered background Image

@shnuffy :smileyvery-happy:

 

@arkadyz.  "I'm curious what happens if you use setBackground on a non-scrolling Manager and add the scrolling one with no background painting on top of it. setBackground, being a built-in feature, might make better use of their hardware acceleration."

 

Similarly squiffy.  I actually tried that first.  I can't see how it can possibly not work.  it makes no sense, the Manager that is displaying the Background image is full screen and non scrollable..... But the evidence was before my eyes!  You will note that I display the current scoll value in pantbackground.  Well, they don't change.  Ever.  And yet the 'centered bitmap moves...  Spooky.  

 

So this looks like an optimization "feature". 

 

But the scroll change listener works!  Result!  Thanks!

 

Here is the "corrected" code:  I don't like the way I have put the listener in, I think I would put it the Manager's constructor and have it listen for changes on itself, but this does demonstrate the 'fix'.

 

public class BackgroundImageScreen extends MainScreen
                                    implements ScrollChangeListener {    
    private int backgroundColor = 0x00FF0000;
    private Bitmap _backgroundBitmap = Bitmap.getPredefinedBitmap(Bitmap.EXCLAMATION);
   
    // Constructor
    public BackgroundImageScreen()
    {         
        super(Manager.USE_ALL_WIDTH | Manager.USE_ALL_HEIGHT | Manager.NO_VERTICAL_SCROLL | Manager.NO_VERTICAL_SCROLLBAR);
        setTitle(new LabelField("Background Image Demo"));          
        VerticalFieldManager backgoundManager = new VerticalFieldManager(Manager.USE_ALL_WIDTH | Manager.USE_ALL_HEIGHT | Manager.VERTICAL_SCROLL | Manager.VERTICAL_SCROLLBAR ) {
            public void paintBackground(Graphics g) {
                int currentVackgroundColor = g.getBackgroundColor();
                try {
                    g.setBackgroundColor(backgroundColor);
                    g.clear();
                } finally {
                    g.setBackgroundColor(currentVackgroundColor);
                }
                int managerWidth = this.getWidth();
                int managerHeight = this.getHeight();
                int centerX = (this.getWidth() - _backgroundBitmap.getWidth()) /2;
                int centerY = (managerHeight - _backgroundBitmap.getHeight()) /2;
                System.out.println("Current positioning: " + Integer.toString(centerX) + ":" + Integer.toString(centerY) +
                                    ", Manager extent: " + Integer.toString(managerWidth) + ":" + Integer.toString(managerHeight) +
                                    ", Manager virtual: " + Integer.toString(this.getVirtualWidth()) + ":" + Integer.toString(this.getVirtualHeight()) +
                                    ", ManagerScroll: " + Integer.toString(this.getHorizontalScroll()) + ":" + Integer.toString(this.getVerticalScroll()));
                g.drawBitmap(this.getHorizontalScroll() + centerX, this.getVerticalScroll() + centerY, _backgroundBitmap.getWidth(), _backgroundBitmap.getHeight(), _backgroundBitmap, 0, 0);
                   
            }

        };
        backgoundManager.setScrollListener(this);
        backgoundManager.add(new EditField("Scroll test: ", "Enter text in here to make it scroll"));
        backgoundManager.add(new SeparatorField());
        backgoundManager.add(new BitmapField(Bitmap.getPredefinedBitmap(Bitmap.QUESTION)));
        backgoundManager.add(new SeparatorField());
        backgoundManager.add(new EditField("Another Scroll test: ", "Enter text in here to make it scroll"));
        this.add(backgoundManager);

    }

        public void scrollChanged( Manager manager, int newHorizontalScroll, int newVerticalScroll ) {
            manager.invalidate();
        }

 

Hope this helps someone else.

Please use plain text.
Developer
QuiteSimple
Posts: 1,452
Registered: ‎11-06-2009
My Device: Torch 9810

Re: Struggling with centered background Image

Sorry for digging old and resolved thread, but what is the reason for not using the standard API background?

 

Background bg = BackgroundFactory.createBitmapBackground(bitmap, Background.POSITION_X_CENTER,
				Background.POSITION_Y_CENTER, Background.REPEAT_NONE);

 



"When you become a level 3 developer, you learn to communicate over the ether. I'm told that level 5 developers are ascend into a higher level of existence beyond the physical realm." gord888 @ crackberry
Please use plain text.
Developer
peter_strange
Posts: 19,608
Registered: ‎07-14-2008
My Device: Not Specified

Re: Struggling with centered background Image

Don't think it helps centering things like Buttons....

Please use plain text.
Developer
QuiteSimple
Posts: 1,452
Registered: ‎11-06-2009
My Device: Torch 9810

Re: Struggling with centered background Image

Ah, stupid me... Got your point, thanks!



"When you become a level 3 developer, you learn to communicate over the ether. I'm told that level 5 developers are ascend into a higher level of existence beyond the physical realm." gord888 @ crackberry
Please use plain text.