【问题标题】:How to detect the device orientation with SensorEventListener?如何使用 SensorEventListener 检测设备方向?
【发布时间】:2015-10-07 04:09:56
【问题描述】:

我想通过实现 SensorEventListener 来检测设备屏幕方向,因为它的当前屏幕方向默认设置为纵向。我需要这样做,因为我的布局包括一些应该独立于其布局旋转的按钮,并且这样做的唯一方法(据我所知)是通过覆盖 onConfigurationChanged 并将相应的动画添加到每个屏幕方向。我不认为 OrientationEventListener 会起作用,因为设置的方向固定为纵向。那么如何从传感器本身检索屏幕方向或角度旋转?

【问题讨论】:

  • 这不是我需要的。我需要检测设备运动以根据它旋转按钮。布局保持原样。
  • 设备运动是什么意思?如果设备不是平的,那么您可以使用上面的链接获取角度,这将告诉您设备屏幕的方向。您可以通过不将活动设置为纵向模式进行测试,然后旋转屏幕以查看角度它将翻转为横向。它可能取决于设备。
  • 这就是问题所在,设备需要设置为纵向模式,因为我不是要创建不同的布局,而是要在不改变布局方向的情况下根据传感器播放按钮动画。移动设备时检查 VSCO Cam 相机活动按钮旋转,以及布局如何保持不变。
  • 我并不是要您不要在生产中将您的活动设置为纵向模式。我只是告诉您在不将活动设置为纵向的情况下进行测试,然后旋转设备。当屏幕将您的活动从纵向更改为横向时,请注意从 OrientationEventListener 返回的角度。所以在这个角度,设备从纵向翻转到横向。现在将您的活动设置为纵向,当 onOrientationChanged 返回时,此角度会旋转您的按钮。或者你可以决定它在哪个角度是横向的,并在 onOrientationChanged 处于这个角度时旋转它。

标签: imagebutton screen-orientation android-sensors sensormanager rotateanimation


【解决方案1】:

OrientationEventListener 可以工作,即使是固定方向;见https://stackoverflow.com/a/8260007/1382108。它根据文档监控传感器。 假设您定义了以下常量:

private static final int THRESHOLD = 40;
public static final int PORTRAIT = 0;
public static final int LANDSCAPE = 270;
public static final int REVERSE_PORTRAIT = 180;
public static final int REVERSE_LANDSCAPE = 90;
private int lastRotatedTo = 0;

这些数字对应于 OrientationEventListener 返回的内容,因此如果您有自然景观设备(平板电脑),则必须考虑到这一点,请参阅How to check device natural (default) orientation on Android (i.e. get landscape for e.g., Motorola Charm or Flipout)

 @Override
public void onOrientationChanged(int orientation) {
    int newRotateTo = lastRotatedTo;
    if(orientation >= 360 + PORTRAIT - THRESHOLD && orientation < 360 ||
            orientation >= 0 && orientation <= PORTRAIT + THRESHOLD)
        newRotateTo = 0;
    else if(orientation >= LANDSCAPE - THRESHOLD && orientation <= LANDSCAPE + THRESHOLD)
        newRotateTo = 90;
    else if(orientation >= REVERSE_PORTRAIT - THRESHOLD && orientation <= REVERSE_PORTRAIT + THRESHOLD)
        newRotateTo = 180;
    else if(orientation >= REVERSE_LANDSCAPE - THRESHOLD && orientation <= REVERSE_LANDSCAPE + THRESHOLD)
        newRotateTo = -90;
    if(newRotateTo != lastRotatedTo) {
        rotateButtons(lastRotatedTo, newRotateTo);
        lastRotatedTo = newRotateTo;
    }
}

rotateButtons 函数类似于:

public void rotateButtons(int from, int to) {

    int buttons[] = {R.id.buttonA, R.id.buttonB};
    for(int i = 0; i < buttons.length; i++) {
        RotateAnimation rotateAnimation = new RotateAnimation(from, to, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        rotateAnimation.setInterpolator(new LinearInterpolator());
        rotateAnimation.setDuration(200);
        rotateAnimation.setFillAfter(true);
        View v = findViewById(buttons[i]);
        if(v != null) {
            v.startAnimation(rotateAnimation);
        }
    }
}

【讨论】:

    猜你喜欢
    • 2023-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-09
    • 1970-01-01
    • 1970-01-01
    • 2013-10-29
    • 2013-11-11
    相关资源
    最近更新 更多