【问题标题】:How to convert "onOrientationChanged" value to landscape and portrait如何将“onOrientationChanged”值转换为横向和纵向
【发布时间】:2013-08-06 07:44:16
【问题描述】:

如何将 onOrientationChanged 方法 orientation 值转换为横向和纵向以了解设备方向。

每当设备方向发生变化时,我都会调用以下函数。
public void onOrientationChanged(int orientation)

参数orientation在0到359之间。
那么,如果设备处于横向模式或纵向模式,我如何从这些值中获知呢?

【问题讨论】:

标签: android android-orientation


【解决方案1】:

你可以使用这个来获得方向

if(getResources().getConfiguration().orientation==Configuration.ORIENTATION_PORTRAIT)
{
  // do your stuff          
}
else
{
   // do your stuff         
}

【讨论】:

  • 我无法使用 onConfig 更改方法,因为我的应用程序正在使用设置方向。所以我正在使用 onOrientationChanged 方法。
【解决方案2】:

您可以使用以下代码在运行时获取方向:

Display display = ((WindowManager) getSystemService(WINDOW_SERVICE)).getDefaultDisplay();
//orientation will contain Surface.ROTATION_0 (no rotation), Surface.ROTATION_90, Surface.ROTATION_180 or Surface.ROTATION_270
int orientation = display.getRotation();

所以,如果你想在改变方向时做点什么,你可以覆盖onConfigurationChanged 方法。

如果你想在运行时改变方向,你可以使用这个(例如,改为纵向):

setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

如果想了解更多详情,请阅读this

【讨论】:

  • getRotation 每次都返回 0。
  • 把它放在 onConfigurationChanged 上,你会看到它的变化..但大多数情况下,纵向为 0 或 3..0,横向为 3
猜你喜欢
  • 1970-01-01
  • 2023-04-02
  • 2020-02-21
  • 2023-01-30
  • 1970-01-01
  • 2018-08-30
  • 1970-01-01
  • 2014-09-16
  • 1970-01-01
相关资源
最近更新 更多