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
tmeerkat
Posts: 22
Registered: ‎11-25-2009
My Device: Not Specified
Accepted Solution

Resizing math

I am resizing an image to fit snugly in its location. I have a set of buttons in a VerticalFieldManager labled "buttons" on the left, and the image (named "displayImage") goes to the right. I want the image to either take up all the vertical space, leaving horizontal space white, or take up all the horizontal space, leaving vertical space white. In other words, I want the image to be as large as it can, without flowing off the screen or being taller than the buttons. I have the following code to do so:

 

//    Scale the image
        int scale;
       
        if(displayImage!=null&&displayImage.getHeight()>0&&displayImage.getWidth()>0)
        {
            double w0 = Display.getWidth();
            double h1 = buttons.getPreferredHeight();
            double w1 = buttons.getPreferredWidth();
            double h2 = displayImage.getHeight();
            double w2 = displayImage.getWidth();
           
            //    Determine the maximum size of the image
            if(h1/h2<=(w0-w1)/w2)
                scale = (int)((h1/h2)*100.0);
            else
                scale = (int)(((w0-w1)/w2)*100.0);
               
            Status.show("height: "+h1+"\nwidth: "+(w0-w1));
            //    Scale and set the image.
            bField.setBitmap(BitmapTools.scale(displayImage,scale));
            right.add(bField);
            Status.show("height: "+BitmapTools.scale(displayImage,scale).getHeight()+"\nwidth: "+BitmapTools.scale(displayImage,scale).getWidth());

 

When I run the application, the buttons height/width displays as 80, 263 (the first Status.show()), and the image height and width after resizing show as 79, 204. Thus, according to these numbers, the buttons and the image should be the same height, and the image should be slightly shorter than the screen width. However, the image height is actually only about 3/4 the height of the buttons.

 

Since the math is obviously correct, I am assuming that I must be using an incorrect method to get the height and width of the buttons or the image. However, if I add another button, the height of the buttons changes while the width stays the same. If I change the image, the height and width change.

 

Does anybody know why the heights can say that they are virtually equal (within a pixle), when visually they are quite different?

 

Please use plain text.
Developer
tmeerkat
Posts: 22
Registered: ‎11-25-2009
My Device: Not Specified

Re: Resizing math

Additional note: I just added a ViewImage class that shows the image at full screen, and this works perfectly with multiple images using the resize method. This leads me to believe that the methods for getting the height of the VerticalFieldManager is not returning the correct height. Any of the other methods for getting the height that I have tried all return 0. Thus, I used getPreferredHeight().

 

Is there a way to get the actual height of a VerticalFieldManager with buttons?

Please use plain text.
Developer
rcmaniac25
Posts: 1,804
Registered: ‎04-28-2009
My Device: Z10 (STL100-4)-10.2.1.2174, Z10 (STL100-3)-10.3.0.700 Dev OS, Z30 (STA100-5)-10.3.0.700 Dev OS, PlayBook (16GB)-2.1.0.1917
My Carrier: Verizon

Re: Resizing math

getPreferredWidth/Height gets the minimum size the button needs, a Manager tells it what size it is allowed to use for the screen. If the available space is smaller then the preferred space then it will be smaller then the preferred size and the content gets cut off, if it has more space then it will be bigger then preferred space. To get the actual width and height you call the same methods as displayImage.

---Spends time in #blackberrydev on freenode (IRC)----
Three simple rules:
1. Please use the search bar before making new posts.
2. "Like" posts that you find helpful.
3. If a solution has been found for your post, mark it as solved.
--I code too much. Well, too bad.
Please use plain text.
Developer
tmeerkat
Posts: 22
Registered: ‎11-25-2009
My Device: Not Specified

Re: Resizing math

Thank you rcmaniac, that is what I initially thought. However, using buttons.getHeight() gives me a 0. The same was true for getWidth(). getPreferredHeight() and width change depending on how many buttons I have loaded.

 

I have checked, and "buttons" is a global object, and it is loaded with buttons in a previous method. It has not, however, been added to the view yet. Could this have something to do with why it is not working? I tried adding the buttons to the screen before adding the image, but I still came up with a size of 0.

Please use plain text.
Developer
rcmaniac25
Posts: 1,804
Registered: ‎04-28-2009
My Device: Z10 (STL100-4)-10.2.1.2174, Z10 (STL100-3)-10.3.0.700 Dev OS, Z30 (STA100-5)-10.3.0.700 Dev OS, PlayBook (16GB)-2.1.0.1917
My Carrier: Verizon

Re: Resizing math

Yea I forgot about the timing issue. getHeight/getWidth return the width and height of the "extent" which is the actual size of the field. The extent is not set until the field's Layout function is called. If your bitmap is scaled in the constructor then the extent will have a width and height of zero but if you call it in the button's layout function then it should return the actual width and height.

 

The only problem with setting it in layout is that if it changes the extent of the field I think it recalls the parent's layout function which will cause your layout function to be called again, this will continue until the width and height don't change. At least that is what I remember reading in a couple posts.

---Spends time in #blackberrydev on freenode (IRC)----
Three simple rules:
1. Please use the search bar before making new posts.
2. "Like" posts that you find helpful.
3. If a solution has been found for your post, mark it as solved.
--I code too much. Well, too bad.
Please use plain text.
Developer
tmeerkat
Posts: 22
Registered: ‎11-25-2009
My Device: Not Specified

Re: Resizing math

I have tried viewing the getWidth() and getHeight() at various points (immediately after adding the buttons to the container, immediately after adding the container to the screen, after adding all items to the screen), but keep getting 0. If I could get an actual value, I wouldn't be too worried about the value changing. After adding, I don't change the display without deleting everything first. I could grab the values in a global variable and use them, but I have yet to find where the methods actually return a useful value.

 

Thank you for all your help. It is greatly appreciated.

Please use plain text.
Developer
rcmaniac25
Posts: 1,804
Registered: ‎04-28-2009
My Device: Z10 (STL100-4)-10.2.1.2174, Z10 (STL100-3)-10.3.0.700 Dev OS, Z30 (STA100-5)-10.3.0.700 Dev OS, PlayBook (16GB)-2.1.0.1917
My Carrier: Verizon

Re: Resizing math

I found this:

http://supportforums.blackberry.com/t5/Java-Development/getField-getWidth-etc-methods-returning-0-fo...

 

It is short but it might help you.

---Spends time in #blackberrydev on freenode (IRC)----
Three simple rules:
1. Please use the search bar before making new posts.
2. "Like" posts that you find helpful.
3. If a solution has been found for your post, mark it as solved.
--I code too much. Well, too bad.
Please use plain text.
Developer
tmeerkat
Posts: 22
Registered: ‎11-25-2009
My Device: Not Specified

Re: Resizing math

Unfortunately, I got 0 doing that as well. I finally had to be content with making a couple constants, and adjusting them so they worked well. Not an elegant or ideal solution, but it allows me to continue on with development.

 

I will leave this thread open for a week, in case someone else has any more insight, and then will close it. However, the issue is solved for me. Once again, thank you everyone for your suggestions.

Please use plain text.
Developer
rcmaniac25
Posts: 1,804
Registered: ‎04-28-2009
My Device: Z10 (STL100-4)-10.2.1.2174, Z10 (STL100-3)-10.3.0.700 Dev OS, Z30 (STA100-5)-10.3.0.700 Dev OS, PlayBook (16GB)-2.1.0.1917
My Carrier: Verizon

Re: Resizing math

Sorry, I tried. Good luck figuring that out.

---Spends time in #blackberrydev on freenode (IRC)----
Three simple rules:
1. Please use the search bar before making new posts.
2. "Like" posts that you find helpful.
3. If a solution has been found for your post, mark it as solved.
--I code too much. Well, too bad.
Please use plain text.
Developer
tmeerkat
Posts: 22
Registered: ‎11-25-2009
My Device: Not Specified

Re: Resizing math

I greatly appreciate the effort. Thank you.

Please use plain text.