温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Android中如何使用重力传感器实现横竖屏放向切换功能

发布时间:2021-07-10 11:02:14 来源:亿速云 阅读:360 作者:小新 栏目:移动开发

小编给大家分享一下Android中如何使用重力传感器实现横竖屏放向切换功能,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!

具体如下:

最近项目中用到了vr视频播放,因为自己实现,同时要实现横竖屏自动切换屏幕,核心代码如下:

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.
 * 监听重力系统传感器的变化,为Vr视频播放器而定制
 */
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, "转到了横屏");
          if(!MySensorHelper.this.isLandLock) {
            Activity mActivity = (Activity)MySensorHelper.this.mActivityWeakRef.get();
            if(mActivity != null) {
              Log.e(MySensorHelper.TAG, "转到了横屏##################");
              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, "转到了竖屏");
          if(!MySensorHelper.this.isPortLock) {
            Activity mActivity = (Activity)MySensorHelper.this.mActivityWeakRef.get();
            if(mActivity != null) {
              Log.e(MySensorHelper.TAG, "转到了竖屏!!!!!!!!!!!!!!!!!!!!!!");
              mActivity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
              isPortLock=true;
              isLandLock=false;
            }
          }
        }
      }
    };
   //this.disable();
  }
  //禁用切换屏幕的开关
  public void disable() {
    Log.e(TAG, "disable");
    this.mPortOrientationListener.disable();
    this.mLandOrientationListener.disable();
  }
  //开启横竖屏切换的开关
  public void enable(){
    this.mPortOrientationListener.enable();
    this.mLandOrientationListener.enable();
  }
  //设置竖屏是否上锁,true锁定屏幕,fanle解锁
  public void setPortLock(boolean lockFlag) {
    this.isPortLock = lockFlag;
  }
  //设置横屏是否锁定,true锁定,false解锁
  public void setLandLock(boolean isLandLock){
    this.isLandLock=isLandLock;
  }
}

使用时将当前activity对象传过来即可,但要在activity的ondestory()方法里面或者back键的监听里面禁用屏幕监听,否则会造成activity不能被回收而导致内存泄漏

helper.disable();

以上是“Android中如何使用重力传感器实现横竖屏放向切换功能”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注亿速云行业资讯频道!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI