【问题标题】:How can I detect screen rotation?如何检测屏幕旋转?
【发布时间】:2011-08-01 09:06:17
【问题描述】:

是否可以检测屏幕旋转?我的意思是 - 仅轮换,这与活动初始化与另一个活动有明显区别?

onXxx 方法似乎对此没有用,我尝试从starting Intent 添加/删除flag(删除似乎没有反映,旋转标志在那里),并尝试添加android:configChanges="orientation" 用于清单中的活动,但是 onConfigurationChanged 方法似乎每隔一秒就会被调用一次......有线。

我想我遗漏了一些东西......但在其他相关线程中没有找到明确的解决方案。

有什么想法吗?

【问题讨论】:

标签: android rotation


【解决方案1】:

清单:

<activity android:name=".MyActivity" android:configChanges="screenSize|orientation|screenLayout|navigation"/>

活动:

@Override
public void onConfigurationChanged(Configuration newConfig)
{
    Log.d("tag", "config changed");
    super.onConfigurationChanged(newConfig);

    int orientation = newConfig.orientation;
    if (orientation == Configuration.ORIENTATION_PORTRAIT)
        Log.d("tag", "Portrait");
    else if (orientation == Configuration.ORIENTATION_LANDSCAPE)
        Log.d("tag", "Landscape");
    else
        Log.w("tag", "other: " + orientation);

    ....
}

也试试这个链接

How do I detect screen rotation

【讨论】:

  • 感谢您的回复。但是,在发布问题之前,我已经尝试过了,这对我不起作用,不知道为什么,但是每次都不会调用 onConfigurationChanged。仅当方向与原始方向相同时(每隔一次)才调用它。
  • 你在哪里测试它?你可以发布你的代码吗?因为我在手机上对其进行了测试,它有效
  • 我正在模拟器上测试它并查看 LogCat 视图。代码与上面发布的代码 100% 相同,没有方向检查:清单中的 android:configChanges="orientation|keyboard" (仅用于此活动)和 @Override public void onConfigurationChanged(配置 newConfig) { super.onConfigurationChanged(newConfig); Log.d(" - ", "onConfigChanged"); // TODO remove } 在activity类中...
  • 附加信息:这是 LogCat 中的输出:Activity first init: 08-01 10:50:11.920: DEBUG/-(279): onCreate 08-01 10:50:11.940: DEBUG/-(279): onStart First rotation: 08-01 10:51:13.420: DEBUG/-(279): onPause 08-01 10:51:13.450: DEBUG/-(279): onCreate 08-01 10:51:13.510: DEBUG/-(279): onStart 08-01 10:51:13.510: DEBUG/-(279): onRestore Second rotation: 08-01 10:52:37.970: DEBUG/-(279): onPause 08-01 10:52:37.980: DEBUG/-(279): onCreate 08-01 10:52:38.000: DEBUG/-(279): onStart 08-01 10:52:38.153: DEBUG/-(279): onConfigChanged
  • jum.. 这就是为什么...对我来说,它在模拟器上不起作用,但在真手机上却可以。也许你应该看看是否有任何关于它的错误......
【解决方案2】:

使用 onConfigurationChanged 方法来检测屏幕旋转不是一个好主意。当用户旋转屏幕时,此活动中的配置更改将无法正常工作。

所以我用这个方案来解决这个问题。

public class SampleActivity extends AppCompatActivity {
    public static final String KEY_LAST_ORIENTATION = "last_orientation";
    private int lastOrientation;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);

        if (savedInstanceState == null) {
            lastOrientation = getResources().getConfiguration().orientation;
        }
    }

    @Override
    protected void onStart() {
        super.onStart();
        checkOrientationChanged();
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        lastOrientation = savedInstanceState.getInt(KEY_LAST_ORIENTATION);
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putInt(KEY_LAST_ORIENTATION, lastOrientation);
    }

    private void checkOrientationChanged() {
        int currentOrientation = getResources().getConfiguration().orientation;
        if (currentOrientation != lastOrientation) {
            onScreenOrientationChanged(currentOrientation);
            lastOrientation = currentOrientation;
        }
    }

    public void onScreenOrientationChanged(int currentOrientation) {
        // Do something here when screen orientation changed
    }
}

但是代码仍然不够好,无法在我的项目中使用它,所以我将此代码应用到我自己的库中 (https://github.com/akexorcist/ScreenOrientationHelper)。

compile 'com.akexorcist:screenorientationhelper:<latest_version>'

您可以像这样创建基本活动类

    public class BaseActivity extends AppCompatActivity implements ScreenOrientationHelper.ScreenOrientationChangeListener {
    private ScreenOrientationHelper helper = new ScreenOrientationHelper(this);

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        helper.onCreate(savedInstanceState);
        helper.setScreenOrientationChangeListener(this);
    }

    @Override
    protected void onStart() {
        super.onStart();
        helper.onStart();
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        helper.onSaveInstanceState(outState);
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        helper.onRestoreInstanceState(savedInstanceState);
    }

    @Override
    public void onScreenOrientationChanged(int orientation) {

    }
}

然后用这个基类扩展activity

public class MainActivity extends BaseActivity {

    ...

    @Override
    public void onScreenOrientationChanged(int orientation) {
        // Do something when screen orientation changed
    }
}

完成!

【讨论】:

    【解决方案3】:

    你为什么不试试这个?

    onCreated中获取手机的方向:

    Display display = ((WindowManager)getSystemService(WINDOW_SERVICE)).getDefaultDisplay();
    myOrientation = display.getOrientation();
    

    然后,覆盖方法 onConfigurationChanged 并检查方向是否已更改:

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        if(newConfig.orientation != myOrientation)
            Log.v(tag, "rotated");
        super.onConfigurationChanged(newConfig);
    }
    

    不要忘记将android:configChanges="orientation" 添加到活动的清单中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-18
      相关资源
      最近更新 更多