我想你几乎明白了。我不知道您是否会做类似的事情,但是这个可以让您在方向更改后立即更改 UI。我做了这个例子只是为了改变 from ROTATION_270
to ROTATION_90
,但你可以添加到代码中,这样我就可以涵盖从 to 等的所有情况PORTRAIT
......REVERSE_PORTRAIT
我的例子只是在方向改变时将文本视图从“横向”更改为“反向横向”。
XML
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" />
</RelativeLayout>
代码
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Display;
import android.view.OrientationEventListener;
import android.view.Surface;
import android.widget.TextView;
public class LandScapeToLandScapeReverse extends Activity
{
private int mCurrentOrientation;
private Display mDisplay;
private OrientationEventListener mOrientationEventListener;
private TextView mTextView;
private static final String TAG = "LandScape To LandScape Reverse";
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
Log.d(TAG, "onCreate");
setContentView(R.layout.activity_main);
mTextView = (TextView) findViewById(R.id.textview);
mDisplay = getWindowManager().getDefaultDisplay();
mCurrentOrientation = mDisplay.getRotation();
mTextView.setText("CurrentOrientation: " + mCurrentOrientation);
mOrientationEventListener = new OrientationEventListener(this)
{
@Override
public void onOrientationChanged(int orientation)
{
// On my phone it seem to change at 233 maybe should start lower
// to take into account of phones model variations.
if (mCurrentOrientation == Surface.ROTATION_270 && orientation > 230)
{
mTextView.setText("CurrentOrientation: " + orientation);
Log.d(TAG, "orientation = " + orientation
+ " getRotation: " + mDisplay.getRotation());
if (mDisplay.getRotation() != mCurrentOrientation)
{
// I think one should disable listener here and enable
// again after doing all the UI changes.
mOrientationEventListener.disable();
mTextView.setText("Landscape reverse");
}
}
}
};
mOrientationEventListener.enable();
}
@Override
protected void onDestroy()
{
super.onDestroy();
Log.d(TAG, "onDestroy");
mOrientationEventListener.disable();
}
}
我有一些想法,也许在我们可以覆盖的方向更改之前调用了一个函数。我会玩,如果我找到一个,我会更新答案。