本文介绍了捕获方向改变事件发生前的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是简单的方向锁定为纵向或横向。

It's simple to lock the orientation to portrait or landscape.

<activity android:name=".MyActivity"
           android:screenOrientation="portrait">

或捕捉到的时候,屏幕旋转:

or to capture when the screen rotates:

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}

但我怎么能抓住一个旋转的事件,它发生过吗?我想控制我的布局会发生什么,如果方向改变,重写的实际旋转屏幕的默认行为。这可能吗?

But how can I capture a rotation event, before it happens? I want to control what happens on my layout if the orientation changes, overriding the default behavior of actually rotating the screen. Is it possible?

推荐答案

使用这块code在的onPause()方法。当rotatio变化,这意味着上创建activty重新coustructs它的自我再次打来电话,previous活动结束();

Use this piece of code in onPause() method. When rotatio changes, activty re-coustructs it self which means on-Create is called again and previous activity is finish();

    int currentOrientation = getResources().getConfiguration().orientation;
    if (currentOrientation == Configuration.ORIENTATION_LANDSCAPE){
        if (display.getRotation() == Surface.ROTATION_0)
        // play with different angles e.g ROTATION_90, ROTATION_180
    }

所以,当旋转改变了previous activites的的onPause()将被调用,在这个阶段你可以决定你想要什么新的方向。

So when rotation changes the previous activites's onPause() will be called and at that stage you can decide what you want with new orientation.

这篇关于捕获方向改变事件发生前的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-15 22:47