2011-08-01 59 views
7

是否可以检测屏幕旋转?我的意思是 - 仅限旋转,与其他活动的活动初始化明显区分开来?如何检测屏幕旋转?

的onXxx方法似乎不是这个有用,我已经尝试添加/从starting Intent删除flag(去除似乎并没有得到体现,在旋转的标志是存在的),并尝试添加机器人:对于清单中的活动,configChanges =“orientation”,但onConfigurationChanged方法似乎每隔一秒旋转一次...有线。

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

任何想法?

+0

你的意思是,如果它从风景切换到肖像或反之亦然? – Pengume

+0

是不是这个线程相同? :http://stackoverflow.com/questions/4843809/how-do-i-detect-screen-rotation – Viren

+0

在onConfigurationChanged方法中,您可以检测哪种类型的ConfigurationChanged发生,如屏幕旋转。 – mikepenz

回答

11

清单:

<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

+0

谢谢你的答复。然而,我已经尝试过了,在发布这个问题之前,这不适用于我,不知道为什么,但onConfigurationChanged不是每次都调用。只有当方向与原始方向相同时(每隔两秒)才会调用它。 – MerlinBG

+0

你在哪里测试它?你能发布你的代码吗?原因我在手机上测试过它,它工作 – Finuka

+0

我正在模拟器上测试它并查看LogCat视图。该代码是100%相同,上面贴无定向检查: android:configChanges="orientation|keyboard" 清单中(仅适用于本次活动),并在活动课 @Override \t public void onConfigurationChanged(Configuration newConfig) { \t \t super.onConfigurationChanged(newConfig); \t \t \t \t Log.d(" - ", "onConfigChanged"); // TODO remove \t } ... – MerlinBG

0

你为什么不试试呢?

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"中。

1

使用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) { 

    } 
} 

然后用这个基类

public class MainActivity extends BaseActivity { 

    ... 

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

完成延长活动!