如何捕获BlackBerry Torch 9800 OS 6的旋转屏幕事件?

时间:2011-07-14 07:41:00

标签: blackberry

有人能告诉我如何抓住这个事件吗? 因为当我将手机旋转到横向模式时,应用程序无法正确显示。 谢谢, DUY

2 个答案:

答案 0 :(得分:1)

当发生这种情况时,BB UI框架definitelly会为您的屏幕调用layout(int width, int height)。这是因为MainScreen也是Manager,因此它应该在BB UI框架开始绘制之前布局其所有子字段。

因此,在layout()中,您可以跟踪当前的方向状态(使用net.rim.device.api.system.Display.getOrientation())并与之前的状态进行比较。如果更改,则设备刚刚旋转。

答案 1 :(得分:0)

我想出了一个与Arhimed建议的方法不同的方法,它似乎运行良好(我使用它来绘制自定义字段的方式不同 - 当设备倾斜时)。我有一个方法

protected void myOrientation() {
    // portrait is true when dh > dw
    boolean portrait = (Display.getOrientation() == Display.ORIENTATION_PORTRAIT);
    // dw and dh = real horizontal and vertical dimensions of display - regardless of device orientation
    int dw = portrait ? Math.min(Display.getWidth(), Display.getHeight()) : Math.max(Display.getWidth(), Display.getHeight());
    int dh = portrait ? Math.max(Display.getWidth(), Display.getHeight()) : Math.min(Display.getWidth(), Display.getHeight());

    // here I draw my custom Field

    invalidate();
}

并在构造函数中调用它一次,然后在每个Accelerometer事件上调用它:

public class MyScreen extends MainScreen implements AccelerometerListener {

private MyScreen() {
    if (AccelerometerSensor.isSupported()) {
        orientationChannel = AccelerometerSensor.openOrientationDataChannel( Application.getApplication() );
        orientationChannel.addAccelerometerListener(this);
    }
    ....
} 


public void onData(AccelerometerData accData) {
    if (old != accData.getOrientation()) {
        myField.myOrientation();
    }
}

也许这对你有帮助,是的 - 如果键盘在Torch上掉了

,你必须检查一下

此致 亚历