package com.PinusBungeana.shipin; import android.app.Activity; import android.os.Bundle; import android.support.annotation.Nullable; import android.view.OrientationEventListener; import android.view.View; import android.widget.ImageView; public class TestActivity extends Activity implements View.OnClickListener { private OrientationEventListener mOrientationEventListener; private boolean mIsLand = false; // 是否是横屏 private boolean mClick = false; // 是否点击 private boolean mClickLand = true; // 点击进入横屏 private boolean mClickPort = true; // 点击进入竖屏 private ImageView mShrinkScreen;//退出全屏 private ImageView mFullScreen;//进入全屏 @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); requstOrientationEventListener(); } public void unRequstOrientationEventListener() { mOrientationEventListener = null; } /** * 开启监听器 */ private void requstOrientationEventListener() { // 屏幕方向改变监听器 mOrientationEventListener = new OrientationEventListener(this) { @Override public void onOrientationChanged(int rotation) { // 设置竖屏 if ((rotation >= 0 && rotation <= 45) || rotation >= 315) { if (mClick) { if (mIsLand && !mClickLand) { return; } else { mClickPort = true; mClick = false; mIsLand = false; } } else { if (mIsLand) { // exFullScreen();//退出全屏 // mActivity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); mIsLand = false; mClick = false; } } } // 设置横屏 else if ((rotation > 45 && rotation < 315)) { if (mClick) { if (!mIsLand && !mClickPort) { return; } else { mClickLand = true; mClick = false; mIsLand = true; } } else { if (!mIsLand) { // fullScreen();//进入全屏 // mActivity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); mIsLand = true; mClick = false; } } } } }; mOrientationEventListener.enable(); } @Override public void onClick(View v) { mClick = true; if (v == mFullScreen || v == mShrinkScreen) { if (!mIsLand) { // fullScreen();//进入全屏 // mActivity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); mIsLand = true; mClickLand = false; } else { // exFullScreen();//退出全屏 // mActivity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); mIsLand = false; mClickPort = false; } } } }
改善旋转屏幕:
import android.content.Context; import android.view.OrientationEventListener; public abstract class SimpleOrientationEventListener extends OrientationEventListener { public static final int ORIENTATION_PORTRAIT = 0; public static final int ORIENTATION_LANDSCAPE = 1; public static final int ORIENTATION_PORTRAIT_REVERSE = 2; public static final int ORIENTATION_LANDSCAPE_REVERSE = 3; private int lastOrientation = 0; public SimpleOrientationEventListener(Context context) { super(context); } public SimpleOrientationEventListener(Context context, int rate) { super(context, rate); } @Override public final void onOrientationChanged(int orientation) { if (orientation < 0) { return; } int curOrientation; if (orientation <= 45) { curOrientation = ORIENTATION_PORTRAIT; } else if (orientation <= 135) { curOrientation = ORIENTATION_LANDSCAPE_REVERSE; } else if (orientation <= 225) { curOrientation = ORIENTATION_PORTRAIT_REVERSE; } else if (orientation <= 315) { curOrientation = ORIENTATION_LANDSCAPE; } else { curOrientation = ORIENTATION_PORTRAIT; } if (curOrientation != lastOrientation) { onChanged(lastOrientation, curOrientation); lastOrientation = curOrientation; } } public abstract void onChanged(int lastOrientation, int orientation); }
使用改善:
public class TestActivity extends Activity { private OrientationEventListener mOrientationEventListener; @Override public void onBackPressed() { super.onBackPressed(); requestOrientationEventListener(); } @Override protected void onDestroy() { super.onDestroy(); unRequestOrientationEventListener(); } private void requestOrientationEventListener() { mOrientationEventListener = new SimpleOrientationEventListener(this) { @Override public void onChanged(int lastOrientation, int orientation) { Loger.e("lastOrientation=" + lastOrientation + " orientation=" + orientation); if (orientation == SimpleOrientationEventListener.ORIENTATION_PORTRAIT) { TestActivity.this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); // exFullScreen();//退出全屏 } else if (orientation == SimpleOrientationEventListener.ORIENTATION_LANDSCAPE) { TestActivity.this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); // fullScreen();//进入全屏 } else if (orientation == SimpleOrientationEventListener.ORIENTATION_LANDSCAPE_REVERSE) { TestActivity.this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE); // fullScreen();//进入全屏 } } }; mOrientationEventListener.enable(); } public void unRequestOrientationEventListener() { mOrientationEventListener.disable(); } }
旋转控件:
public abstract class RotateOrientationEventListener extends SimpleOrientationEventListener { public RotateOrientationEventListener(Context context) { super(context); } public RotateOrientationEventListener(Context context, int rate) { super(context, rate); } @Override public final void onChanged(int lastOrientation, int orientation) { int startDeg = lastOrientation == 0 ? orientation == 3 ? 360 : 0 : lastOrientation == 1 ? 90 : lastOrientation == 2 ? 180 : 270; // don't know how, but it works int endDeg = orientation == 0 ? lastOrientation == 1 ? 0 : 360 : orientation == 1 ? 90 : orientation == 2 ? 180 : 270; // don't know how, but it works onRotateChanged(startDeg, endDeg); } public abstract void onRotateChanged(int startDeg, int endDeg); }
使用旋转控件:
public class TestActivity extends Activity { private OrientationEventListener mOrientationEventListener; @Override public void onBackPressed() { super.onBackPressed(); requestOrientationEventListener(); } @Override protected void onDestroy() { super.onDestroy(); unRequestOrientationEventListener(); } private void rotateView(View view, int startDeg, int endDeg) { view.setRotation(startDeg); view.animate().rotation(endDeg).start(); } private void requestOrientationEventListener() { mOrientationEventListener = new RotateOrientationEventListener(this) { @Override public void onRotateChanged(int startDeg, int endDeg) { rotateView(view, startDeg, endDeg); } }; mOrientationEventListener.enable(); } public void unRequestOrientationEventListener() { mOrientationEventListener.disable(); } }