I didn't want to tag on to the old thread because this is its own issue, but this is one step past where I was here:
https://groups.google.com/forum/#!topic/codenameone-discussions/PgT-q1RmiIoI changed to a BorderLayout and subclassed Label so that I could override setPreferredSize so that the Label (and the image used as its icon) that lives in the NORTH component area will always be full width (with height proportional to the image size) regardless of device orientation and to resize on device rotation.
The problem that I have remaining is that, on iOS devices, when the device rotates, the image squeezes or stretches to fit the width of the screen as the rest of the interface rotates, but the proportions are off for a fraction of a second, then the image snaps into place with the proper proportions. On Android devices and in the simulator, the interface rotates and the image is properly sized. I only have the problem on actual iOS devices. I have tried several combinations of animate(Fade)Layout(AndWait) both before and after rotation without any positive result.
I am going to include a bunch of the related code in hopes that it will help. Names have been changed to protect the innocent.
Sorry this is so long. I didn't know a better way to get all the pertinent information out there to avoid a lengthy back-and-forth conversation.
In the app's start method:
Display.getInstance().getCurrent().addOrientationListener(
new ActionListener()
{
public void actionPerformed( ActionEvent evt )
{
String viewTag = MyAppData.sectionTagByLocationAndIndex( ICON_LOCATION_TABBAR, appCaller.tabComponent.getSelectedIndex() );
if( !viewTag.equals( "" ) )
{
if( appCaller.childInstances.get( viewTag ) != null )
{
TabViews thisView = (TabViews)appCaller.childInstances.get( viewTag );
thisView.rotate();
}
}
Display.getInstance().getCurrent().animateLayout( 100 );
}
} // new ActionListener()
A child instance's rotate method:
public void rotate()
{
this.imageLabel.resizeToFullWidth();
}
The same child instance's content provider (where the label/image is added to the interface):
Container returnContainer = new Container( new BorderLayout() );
this.imageLabel = new CustomImageLabel();
try
{
Image bannerImage = Image.createImage( "/a_banner_image.png" );
this.imageLabel.setFullWidthImage( bannerImage );
}
catch( IOException e )
{
e.printStackTrace();
}
BrowserComponent bc = new BrowserComponent();
bc.setPage( this.getContent(), "/" );
Style bcStyle = new Style();
bcStyle.setBorder( Border.createEmpty() );
bcStyle.setMargin( 3, 3, 3, 3 );
bcStyle.setFgColor( 0xffffff );
bc.setSelectedStyle( bcStyle );
bc.setUnselectedStyle( bcStyle );
returnContainer.addComponent( BorderLayout.NORTH, this.imageLabel );
returnContainer.addComponent( BorderLayout.CENTER, bc );
return returnContainer;
And the custom image label class:
public class CustomImageLabel extends Label
{
public Image labelImage;
public void setImage( Image inputImage )
{
this.labelImage = inputImage;
}
@Override
public Dimension calcPreferredSize()
{
if( this.labelImage == null )
{
return this.calcPreferredSize();
}
int deviceWidth = Integer.valueOf( MyApp.deviceInfo().get( "width" ) ).intValue();
int imgWidth = this.labelImage.getWidth();
int imgHeight = this.labelImage.getHeight();
double ratio = ((double)imgHeight/(double)imgWidth);
return new Dimension( deviceWidth, (int)Math.floor( (deviceWidth * ratio) ) );
}
public void setFullWidthImage( Image inputImage )
{
this.setImage( inputImage );
Style imgStyle = new Style();
imgStyle.setPadding( 0, 0, 0, 0 );
imgStyle.setMargin( 0, 0, 0, 0 );
imgStyle.setAlignment( Component.CENTER );
this.setSelectedStyle( imgStyle );
this.setUnselectedStyle( imgStyle );
Dimension dim = this.calcPreferredSize();
this.setIcon( this.labelImage.scaled( dim.getWidth(), dim.getHeight() ) );
}
/**
* A convenience wrapper to setFullWidthImage( Image )
*/
public void resizeToFullWidth()
{
this.setFullWidthImage( this.labelImage );
}
}
I have an additional problem in this particular code in that, again, only on iOS devices (not on Android or in the simulator), what looks like a 1 pixel border gets added to the bottom of the BrowserComponent every time the device rotates. 2 rotations = 2 pixel border, 3 rotations = 3 pixel border... But I'd be happy to address that in another thread if I can prevent the image from visibly popping into size/shape from the result of this thread.
Thanks!