10-21-2009 07:37 AM
Hi,
I am working with an application which uses banner(image).I have put the code for resizing the images and fields,but it is resizing after a screen refresh ,it should work immediately as I change device's direction.Please help me!!
Solved! Go to Solution.
10-21-2009 01:39 PM
You can hook into orientation changes by overriding the "sublayout" method on your screen (just remember to call the super.sublayout method at the end).
10-21-2009 02:32 PM
10-21-2009 02:42 PM
Something like,
protected void sublayout (int width, int height) {
// your code here
super.sublayout(width, height);
}
or
protected void sublayout (int width, int height) {
super.sublayout(width, height);
// your code here
}
One will run the default layout code after yours. The other will run the default code before yours.
10-22-2009 12:23 AM
Hi,
Its still not working ,
I have put the code given below:
protected void sublayout(int width, int height) {
setExtent(width, height);
super.sublayout(width, height);
}
and tried by putting super above the code too.
Should I have to call sublayout manually(if yes then when to call) or it will be called automatically,please help me!!
10-22-2009 01:26 AM
Hi, am also having the same issue..!! how did you resized your fields?? did u changed all the images width for resizing or is there any API support for this?
plz send me some code snippet of how you implemented this.
10-22-2009 01:31 AM
For image resizing i have used the code given below:
private static int[] rescaleArray(int[] ini, int x, int y, int x2, int y2)
{
int out[] = new int[x2*y2];
for (int yy = 0; yy < y2; yy++)
{
int dy = yy * y / y2;
for (int xx = 0; xx < x2; xx++)
{
int dx = xx * x / x2;
out[(x2 * yy) + xx] = ini[(x * dy) + dx];
}
}
return out;
}
public static Bitmap resizeBitmap(Bitmap image, int width, int height)
{
// Note from DCC:
// an int being 4 bytes is large enough for Alpha/Red/Green/Blue in an 8-bit plane...
// my brain was fried for a little while here because I am used to larger plane sizes for each
// of the color channels....
//
//Need an array (for RGB, with the size of original image)
//
int rgb[] = new int[image.getWidth()*image.getHeight()];
//Get the RGB array of image into "rgb"
//
image.getARGB(rgb, 0, image.getWidth(), 0, 0, image.getWidth(), image.getHeight());
//Call to our function and obtain RGB2
//
int rgb2[] = rescaleArray(rgb, image.getWidth(), image.getHeight(), width, height);
//Create an image with that RGB array
//
Bitmap temp2 = new Bitmap(width, height);
temp2.setARGB(rgb2, 0, width, 0, 0, width, height);
return temp2;
}
public static Bitmap bestFit(Bitmap image, int maxWidth, int maxHeight)
{
// getting image properties
int w = image.getWidth();
int h = image.getHeight();
// get the ratio
int ratiow = 100 * maxWidth / w;
int ratioh = 100 * maxHeight / h;
// this is to find the best ratio to
// resize the image without deformations
int ratio = Math.min(ratiow, ratioh);
// computing final desired dimensions
int desiredWidth = w * ratio / 100;
int desiredHeight = h * ratio / 100;
//resizing
return resizeBitmap(image, desiredWidth, desiredHeight);
}
}
But the issue is still pending,i m not getting resizing as i tilt BB,but after one refresh I get the desired result
10-22-2009 03:38 AM - last edited on 10-22-2009 03:40 AM
Hi trivedirujuta,
Thanks for your code snippet, still having few doubts with me..
1) am using png images, can i resize them by using your logic??
2) how can i resize the fields ( i have different fields on my screen)? how can i allign them accordingly?
3) how can i know when to resize?
can you be more detail plzz....
10-22-2009 08:36 AM
Hello,is there anyone to solve the problem?
10-23-2009 06:45 AM
The following i sonly my opinion, there are a lot better UI designers on this board who know more about this than I do. What you see below is what I have done, it is not necessarily the right or the best thing to do. Hopefully others will tell me what I've done wrong.
There are number of issues here, we need to be clear about which we are addressing:
Issue 1: Detecting when an orientation change has occurred.
As everyone says, you do this in sublayout. Check the width (Display.getWidth()) now and the width when you last laid out your Screen - if different, then you have an orientation change.
Note be careful of length here, it depends on whether the keyboard is being displayed or not.
Issue 2: Deciding how to layout your landscape format screen
This is entirely your responsibility. We can't help here. There is no API that will reformat a screen for you.
Sounds like you have two different layouts depending on the orientation. Then design and implement these, deciding which one to use at the time your Screen is created. If you need to resize Bitmaps or change the width of Fields for the landscape format screen, you should do this. This code is NOT dependent on detecting a change, remember the user could be in Landscape mode when she/he starts your application anyway!
It may seem an overhead to have two different layouts, but in fact, you are probably already altering you layout for devices with different screen sizes anyway, so the concept of designing your layout around the actual dimensions of the Display is not a Storm specific issue.
Issue 3: Changing the format when you detect a orientation change
This is a tricky one. In some screens I've managed to do this by just adjusting the useful width of Fields, for example ListFields, and then they will display themselves making full use of the Width. However other times I have actually wanted to add or remove Fields, for example, remove Header information in Landscape format because the height is so much smaller. Because sublayout is called during a layout, you can't actually change the layout processing at this time, so you can't add or remove. So in these chases, I schedule, using invokeLater, an update of the Screen that will action all the orientation changes.
Note that the user may change the orientation again before this is actioned, so if you do this, you might consider checking in your invokedLater Runnable, that the Orientation has actually changed. Up to you.