黑莓编程的自动旋转

时间:2011-10-17 05:33:33

标签: blackberry rotation

我为Blackberry Storm制作代码。当我的应用程序在水平显示(480x360)时,它的工作。但当黑莓倾斜到垂直(360x480)时,图片被切断了。所以我问如何设置,以便在轮换时,还调整图片大小?有没有办法检查黑莓再次水平或垂直显示?

感谢。

2 个答案:

答案 0 :(得分:6)

以下是两件事,要么您将锁定屏幕方向,要么在您的应用程序中支持。

代码示例:检索屏幕方向

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;
}

代码示例:在BlackBerry API应用程序中强制纵向视图

// Use code like this before invoking UiApplication.pushScreen()
int direction = Display.DIRECTION_NORTH;
Ui.getUiEngineInstance().setAcceptableDirections(direction);

您想要在方向更改时处理图像和其他图形设置,然后您可以在代码中进行以下更改。

  1. 在MainScreen子类中重写sublayout方法。

    protected void sublayout(int arg0,int arg1){             //做所有的             super.sublayout(arg0,arg1);         }

  2. 检查方向更改,重新排列UI。对于这样的事情,建议使用相对布局。

  3. 希望,这可能会帮助你。有关详细信息,请访问Specifying_display_direction_of_screen

    编辑:覆盖sublayout(),然后编写特定于方向的代码

    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);
    }
    

    编辑2:

    由于UI事件锁定,您出错了,现在您对代码进行了以下更改。

    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);
            }
    

答案 1 :(得分:0)

我之前遇到过这个问题并解决了它。解决方案不是“迫使黑莓屏幕不旋转”。我之前做过这件事很烦人。那么解决方案是覆盖主屏幕的subLayout方法和主屏幕中包含的所有管理器。旋转发生时始终会调用此方法。

您的代码如下所示:

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);
}