问题
I make code to Blackberry Storm. When my application in horizontal display (480x360), it's work. But when the Blackberry tilt into Vertical (360x480), the picture is cut off. So I was asking how to set up so that at the time of rotation, also resize the picture? is there any method to check if BlackBerry again horizontal or vertical display?
Thanks.
回答1:
Here are two things, either you will lock screen orientation or you will support in your application.
Code sample: Retrieving screen orientation
switch(Display.getOrientation())
{
case Display.ORIENTATION_LANDSCAPE:
Dialog.alert("Screen orientation is landscape"); break;
case Display.ORIENTATION_PORTRAIT:
Dialog.alert("Screen orientation is portrait"); break;
case Display.ORIENTATION_SQUARE:
Dialog.alert("Screen orientation is square"); break;
default:
Dialog.alert("Screen orientation is not known"); break;
}
Code sample: Forcing portrait view in a BlackBerry API application
// Use code like this before invoking UiApplication.pushScreen()
int direction = Display.DIRECTION_NORTH;
Ui.getUiEngineInstance().setAcceptableDirections(direction);
You you want to handle the images and other graphics setup on orientation change then you can do the following changes in your code.
Override the sublayout method, in your MainScreen subclass.
protected void sublayout(int arg0, int arg1) { // do all the super.sublayout(arg0, arg1); }
Check for the orientation changes, rearrange the UI. Usages of relative layout is recommended for such things.
Hope, this might help you out. For more info visit Specifying_display_direction_of_screen
Edit: override sublayout() and then write the code specific to orientation
public void sublayout(int width, int height) {
switch(Display.getOrientation())
{
case Display.ORIENTATION_LANDSCAPE:
// write the piece of code for refreshing the screen UI which screen orientation is landscape
break;
case Display.ORIENTATION_PORTRAIT:
// write the piece of code for refreshing the screen UI which screen orientation is portrait
break;
}
super.sublayout(width, height);
}
Edit 2:
you were going wrong because of UI event lock, now you do the following changes to your code.
public void sublayout(int maxWidth, int maxHeight) {
int displayWidth = deviceWidth;
int displayHeight = deviceHeight;
switch (Display.getOrientation()) {
case Display.ORIENTATION_LANDSCAPE:
UiApplication.getUiApplication().invokeLater(new Runnable()
{
public void run()
{
Dialog.alert("Screen orientation is landscape");
// here you need to change your uI code as you want to do in for landscape mode
// you may need to delete and add the UI comps manually
// if the components added to absolute layout then just refresh the screen it will auto adjust with your new screen size
}
});
break;
case Display.ORIENTATION_PORTRAIT:
UiApplication.getUiApplication().invokeLater(new Runnable()
{
public void run()
{
Dialog.alert("Screen orientation is PORTRAIT:");
// here you need to change your uI code as you want to do in for PORTRAIT mode
// you may need to delete and add the UI comps manually
// if the components added to absolute layout then just refresh the screen it will auto adjust with your new screen size
}
});
break;
}
super.sublayout(displayWidth, displayHeight);
setExtent(displayWidth, displayHeight);
}
回答2:
Well I faced this problem before and solved it. The solution is not to "oblige the blackberry screen not to rotate". I made this before and it is annoying. Well the solution to this is override the subLayout method of the main screen and all the managers included in the main screen. This method is always called when a rotation occurs.
Your code will looks like follows:
public void sublayout(int width, int height) {
changeImage(); // this method will change your image x and y and then draw it again
super.sublayout(width, height);
}
来源:https://stackoverflow.com/questions/7789734/auto-rotation-on-blackberry-programming