Android Programming Realizes Horizontal and Vertical Screen Orientation Switching Function Based on Gravity Sensor

  • 2021-08-12 03:38:47
  • OfStack

In this paper, the Android programming based on gravity sensor to achieve horizontal and vertical screen orientation switching function. Share it for your reference, as follows:

Recently, vr video playback has been used in the project, because it is realized by itself, and at the same time, it is necessary to realize automatic screen switching between horizontal and vertical screens. The core code is as follows:


package com.d1ev.touch.App.helper;
import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.util.Log;
import android.view.OrientationEventListener;
import java.lang.ref.WeakReference;
/**
 * Created by Administrator on 2016/12/3 0003.
 *  Monitor the change of gravity system sensor, which is Vr Customized for video player 
 */
public class MySensorHelper {
  private static final String TAG = MySensorHelper.class.getSimpleName();
  private OrientationEventListener mLandOrientationListener;
  private OrientationEventListener mPortOrientationListener;
  private WeakReference<Activity> mActivityWeakRef;
  private boolean isPortLock = false;
  private boolean isLandLock=false;
  public MySensorHelper(final Activity activity) {
    this.mActivityWeakRef = new WeakReference(activity);
    this.mLandOrientationListener = new OrientationEventListener(activity, 3) {
      public void onOrientationChanged(int orientation) {
        Log.d(MySensorHelper.TAG, "mLandOrientationListener");
        if(orientation < 100 && orientation > 80 || orientation < 280 && orientation > 260) {
          Log.e(MySensorHelper.TAG, " Turned to the horizontal screen ");
          if(!MySensorHelper.this.isLandLock) {
            Activity mActivity = (Activity)MySensorHelper.this.mActivityWeakRef.get();
            if(mActivity != null) {
              Log.e(MySensorHelper.TAG, " Turned to the horizontal screen ##################");
              mActivity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
              isLandLock=true;
              isPortLock=false;
            }
          }
        }
      }
    };
    this.mPortOrientationListener = new OrientationEventListener(activity, 3) {
      public void onOrientationChanged(int orientation) {
        Log.w(MySensorHelper.TAG, "mPortOrientationListener");
        if(orientation < 10 || orientation > 350 || orientation < 190 && orientation > 170) {
          Log.e(MySensorHelper.TAG, " Turned to the vertical screen ");
          if(!MySensorHelper.this.isPortLock) {
            Activity mActivity = (Activity)MySensorHelper.this.mActivityWeakRef.get();
            if(mActivity != null) {
              Log.e(MySensorHelper.TAG, " Turned to the vertical screen !!!!!!!!!!!!!!!!!!!!!!");
              mActivity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
              isPortLock=true;
              isLandLock=false;
            }
          }
        }
      }
    };
   //this.disable();
  }
  // Disable the switch to switch the screen 
  public void disable() {
    Log.e(TAG, "disable");
    this.mPortOrientationListener.disable();
    this.mLandOrientationListener.disable();
  }
  // Turn on the switch of horizontal and vertical screen switching 
  public void enable(){
    this.mPortOrientationListener.enable();
    this.mLandOrientationListener.enable();
  }
  // Set whether the vertical screen is locked, true Lock screen ,fanle Unlock 
  public void setPortLock(boolean lockFlag) {
    this.isPortLock = lockFlag;
  }
  // Set whether the horizontal screen is locked, true Lock, false Unlock 
  public void setLandLock(boolean isLandLock){
    this.isLandLock=isLandLock;
  }
}

When you use it, you can pass the current activity object, but you should use it in the activity ondestory() Method or back key monitor inside disable screen monitor, otherwise will cause activity can not be recycled and lead to memory leak


helper.disable();

More readers interested in Android can check the topics of this site: "Introduction and Advanced Tutorial of Android Development", "Summary of View Skills in Android View", "Summary of activity Operation Skills in Android Programming", "Summary of Android File Operation Skills", "Summary of Android Resource Operation Skills" and "Summary of Android Control Usage"

I hope this article is helpful to everyone's Android programming.


Related articles: