Android怎样判断当前屏幕是否为横屏状态



一般来说,我们应用中绝大多数页面会需要强制用户横屏或者竖屏,但是少数页面除外,比如说视频播放器页面。


这两天,在做功能的时候,有个需求是判断用户的屏幕是否为横屏,通常我们会使用activity 的getRequestedOrientation()方法也就是


if (getRequestedOrientation() == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) {

java当中,一般有如下几种状态
/**
     * Constant corresponding to <code>unspecified</code> in
     * the {@link android.R.attr#screenOrientation} attribute.
     */
    public static final int SCREEN_ORIENTATION_UNSPECIFIED = -1;
    /**
     * Constant corresponding to <code>landscape</code> in
     * the {@link android.R.attr#screenOrientation} attribute.
     */
    public static final int SCREEN_ORIENTATION_LANDSCAPE = 0;
    /**
     * Constant corresponding to <code>portrait</code> in
     * the {@link android.R.attr#screenOrientation} attribute.
     */
    public static final int SCREEN_ORIENTATION_PORTRAIT = 1;
    /**
     * Constant corresponding to <code>user</code> in
     * the {@link android.R.attr#screenOrientation} attribute.
     */
    public static final int SCREEN_ORIENTATION_USER = 2;
    /**
     * Constant corresponding to <code>behind</code> in
     * the {@link android.R.attr#screenOrientation} attribute.
     */
    public static final int SCREEN_ORIENTATION_BEHIND = 3;
    /**
     * Constant corresponding to <code>sensor</code> in
     * the {@link android.R.attr#screenOrientation} attribute.
     */
    public static final int SCREEN_ORIENTATION_SENSOR = 4;
  
    /**
     * Constant corresponding to <code>nosensor</code> in
     * the {@link android.R.attr#screenOrientation} attribute.
     */
    public static final int SCREEN_ORIENTATION_NOSENSOR = 5;

    /**
     * Constant corresponding to <code>sensorLandscape</code> in
     * the {@link android.R.attr#screenOrientation} attribute.
     */
    public static final int SCREEN_ORIENTATION_SENSOR_LANDSCAPE = 6;

    /**
     * Constant corresponding to <code>sensorPortrait</code> in
     * the {@link android.R.attr#screenOrientation} attribute.
     */
    public static final int SCREEN_ORIENTATION_SENSOR_PORTRAIT = 7;

    /**
     * Constant corresponding to <code>reverseLandscape</code> in
     * the {@link android.R.attr#screenOrientation} attribute.
     */
    public static final int SCREEN_ORIENTATION_REVERSE_LANDSCAPE = 8;

    /**
     * Constant corresponding to <code>reversePortrait</code> in
     * the {@link android.R.attr#screenOrientation} attribute.
     */
    public static final int SCREEN_ORIENTATION_REVERSE_PORTRAIT = 9;

    /**
     * Constant corresponding to <code>fullSensor</code> in
     * the {@link android.R.attr#screenOrientation} attribute.
     */
    public static final int SCREEN_ORIENTATION_FULL_SENSOR = 10;

    /**
     * Constant corresponding to <code>userLandscape</code> in
     * the {@link android.R.attr#screenOrientation} attribute.
     */
    public static final int SCREEN_ORIENTATION_USER_LANDSCAPE = 11;

    /**
     * Constant corresponding to <code>userPortrait</code> in
     * the {@link android.R.attr#screenOrientation} attribute.
     */
    public static final int SCREEN_ORIENTATION_USER_PORTRAIT = 12;

    /**
     * Constant corresponding to <code>fullUser</code> in
     * the {@link android.R.attr#screenOrientation} attribute.
     */
    public static final int SCREEN_ORIENTATION_FULL_USER = 13;

    /**
     * Constant corresponding to <code>locked</code> in
     * the {@link android.R.attr#screenOrientation} attribute.
     */
    public static final int SCREEN_ORIENTATION_LOCKED = 14;


很多时候,我们的屏幕设置的是SCREEN_ORIENTATION_SENSOR,当在这种情况下,就无法判断用户当前的屏幕方向的类型,从网上查了很多资料,最后在官方文档中为大家找到了一种办法】

官方提供了一个监听类   OrientationEventListener 。使用方法如下

OrientationEventListener mScreenOrientationEventListener;//屏幕方向监听
public boolean mScreenExifOrientation;			 <pre name="code" class="java"> 

<span style="white-space:pre">		</span>mScreenOrientationEventListener = new OrientationEventListener(this) {
			
			@Override
			public void onOrientationChanged(int i) {
				// i的范围是0~359 
                <span style="white-space:pre">		</span>// 屏幕左边在顶部的时候 i = 90; 
                <span style="white-space:pre">		</span>// 屏幕顶部在底部的时候 i = 180; 
                <span style="white-space:pre">		</span>// 屏幕右边在底部的时候 i = 270; 
               <span style="white-space:pre">		</span>        // 正常情况默认i = 0; 
   
                <span style="white-space:pre">		</span>if(45 <= i && i < 135) { 
                    <span style="white-space:pre">			</span>mScreenExifOrientation =true; 
               <span style="white-space:pre">			</span>} else if(135 <= i && i < 225) { 
                    <span style="white-space:pre">			</span>mScreenExifOrientation = false; 
                <span style="white-space:pre">		</span>} else if(225 <= i && i < 315) { 
                    <span style="white-space:pre">			</span>mScreenExifOrientation = true; 
                <span style="white-space:pre">		</span>} else { 
                    <span style="white-space:pre">			</span>mScreenExifOrientation = false; 
                <span style="white-space:pre">		</span>} 
			}
		};
		mScreenOrientationEventListener.enable();

 

这样就可以判断当前屏幕的方向了。



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值