2021-08-19 Android app获取设备旋转角度

本文介绍了如何通过OrientationEventListener在Android应用中实时监测手机屏幕的旋转角度变化,通过实例代码展示了如何创建监听器并处理不同角度的事件。同时,探讨了SensorEventListenerImpl在框架层面的实现细节。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、使用OrientationEventListener监测指定的屏幕旋转角度,继承OrientationEventListener类监听手机的旋转角度的变化。

二、直接贴源码吧 MainActivity.java

package com.example.sensordeom;
import androidx.appcompat.app.AppCompatActivity;

import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.util.Log;
import android.view.OrientationEventListener;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import java.util.List;

//1,获得SensorManager对象
//2,获得想要的Sensor对象
//3,绑定监听器
public class MainActivity extends Activity {
    public static final String TAG = "SENSORDEMO";
    Button findBut,accelerationBut,lightBut,orientationBut,proximityBut;
    SensorManager sensorManager;
    TextView text,accText,luxText;
    float gravity[]=new float[3];
    float linear_acceleration[]=new float[3];
    private TextView oriBtn;
    private MyOrientoinListener myOrientoinListener;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        oriBtn = (TextView) findViewById(R.id.orientation);

        myOrientoinListener = new MyOrientoinListener(this);
        myOrientoinListener.enable();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //销毁时取消监听
        myOrientoinListener.disable();
    }

    class MyOrientoinListener extends OrientationEventListener {

        public MyOrientoinListener(Context context) {
            super(context);
        }

        public MyOrientoinListener(Context context, int rate) {
            super(context, rate);
        }

        @Override
        public void onOrientationChanged(int orientation) {
            String hint="";
            int angle=0;
            if (orientation == OrientationEventListener.ORIENTATION_UNKNOWN) {
                hint="手机平放"; //return;  //手机平放时,检测不到有效的角度
            }
            //可以根据不同角度检测处理,这里只检测四个角度的改变
            if (((orientation >= 0) && (orientation < 45)) || (orientation > 315)) { //0度
                angle = 0;
            } else if (orientation > 45 && orientation < 135) { //180度
                angle = 90;
            } else if (orientation > 135 && orientation < 225) { //270度
                angle = 180;
            } else if (orientation > 225 && orientation < 315) { //90度
                angle = 270;
            }
            oriBtn.setText(hint+" orientation="+orientation+"  angle="+angle);
        }
        }
}

三、app测试效果

 

 

 四,分析framework下的主要实现这个功能的源码,详细代码请看frameworks\base\core\java\android\view\OrientationEventListener.java,这里只贴出核心的代码SensorEventListenerImpl,这里会向app发出设备旋转的变动信息。

class SensorEventListenerImpl implements SensorEventListener {
        private static final int _DATA_X = 0;
        private static final int _DATA_Y = 1;
        private static final int _DATA_Z = 2;
        
        public void onSensorChanged(SensorEvent event) {
            float[] values = event.values;
            int orientation = ORIENTATION_UNKNOWN;
            float X = -values[_DATA_X];
            float Y = -values[_DATA_Y];
            float Z = -values[_DATA_Z];        
            float magnitude = X*X + Y*Y;
            // Don't trust the angle if the magnitude is small compared to the y value
            if (magnitude * 4 >= Z*Z) {
                float OneEightyOverPi = 57.29577957855f;
                float angle = (float)Math.atan2(-Y, X) * OneEightyOverPi;
                orientation = 90 - (int)Math.round(angle);
                // normalize to 0 - 359 range
                while (orientation >= 360) {
                    orientation -= 360;
                } 
                while (orientation < 0) {
                    orientation += 360;
                }
            }
            if (mOldListener != null) {
                mOldListener.onSensorChanged(Sensor.TYPE_ACCELEROMETER, event.values);
            }
            if (orientation != mOrientation) {
                mOrientation = orientation;
                onOrientationChanged(orientation);
            }
        }

        public void onAccuracyChanged(Sensor sensor, int accuracy) {

        }
    }

 五、参考文章。

https://www.freesion.com/article/9784538600/

https://blog.youkuaiyun.com/u010029439/article/details/100106687

 https://blog.youkuaiyun.com/youmingyu/article/details/52750374

https://www.jb51.net/article/185034.htm

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值