11-27-2009 04:41 PM
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
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
scale = (int)((h1/h2)*100.0);
scale = (int)(((w0-w1)/w2)*100.0);
Status.show("height: "+h1+"\nwidth: "+(w0-w1));
// Scale and set the image.
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?
Solved! Go to Solution.
11-27-2009 05:16 PM
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?
11-27-2009 05:22 PM
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.
11-27-2009 05:45 PM
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.
11-27-2009 06:03 PM
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.
11-27-2009 06:20 PM
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.
11-27-2009 09:43 PM
I found this:
It is short but it might help you.
11-28-2009 01:34 PM
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.
11-28-2009 10:53 PM
Sorry, I tried. Good luck figuring that out.