sensor:gsensor的流程以及优化分析

本文详细探讨了Android设备中gsensor(重力感应器)的工作流程,包括PhoneWindowManager如何监听传感器变化,以及在接收到传感器事件后如何过滤和处理数据以确定设备的旋转角度。通过分析`onSensorChanged`方法中的倾斜角计算和滤波过程,提出优化转屏速度和精度的策略,如调整TILT_TOLERANCE和ADJACENT_ORIENTATION_ANGLE_GAP等参数。

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

概述

frameworks\base\core\java\android\view\WindowOrientationListener.java

监听sensor是否有数据变化

 

首先看看芯片方向(x,y,z):

计算机生成了可选文字: 汀 OP VIEW )D ! RECTION OF THEDETECTABLEACCELERAT ! ONS

补充流程:

PhoneWindowManager调用updateOrientationListenerLp去SensorManager里面去注册一个Listener,

当Sensor发生变化的时候,PhoneWindowManager就可以监听到并且调用回调onProposeRotationChanged

Callback 如下:

1 MyOrientationListener.onProposeRotationChange  PhoneWindowManager.java

-------------

    class MyOrientationListener extends WindowOrientationListener {

       

        @Override

        public void onProposedRotationChanged(introtation) {

            if (localLOGV) Log.v(TAG,"onProposedRotationChanged, rotation=" + rotation);

            updateRotation(false);

        }

    }

--------

2. PhoneWindowManager.updateRotation(booleanalwaysSendConfiguration)

    void updateRotation(booleanalwaysSendConfiguration) {

        try {

            //set orientation on WindowManager

            mWindowManager.updateRotation(alwaysSendConfiguration, false);、、WindowManagerService的对象

        } catch (RemoteException e) {

            // Ignore

        }

    }

3. WindowManagerService.updateRotation(booleanalwaysSendConfiguration, boolean forceRelayout)

 

 

二代码分析

public void onSensorChanged(SensorEvent event) {

            // The vector given in theSensorEvent points straight up (towards the sky) under ideal

            // conditions (the phone is notaccelerating).  I'll call this up vectorelsewhere.

            float x =event.values[ACCELEROMETER_DATA_X];

            float y =event.values[ACCELEROMETER_DATA_Y];

            float z =event.values[ACCELEROMETER_DATA_Z];

 

            if (LOG) {

                Slog.v(TAG, "Rawacceleration vector: "

                        + "x=" + x +", y=" + y + ", z=" + z

                        + ",magnitude=" + FloatMath.sqrt(x * x + y * y + z * z));

            }

 

            // Apply a low-pass filter to theacceleration up vector in cartesian space.

            // Reset the orientation listenerstate if the samples are too far apart in time

            // or when we see values of (0, 0,0) which indicates that we polled the

            // accelerometer too soon afterturning it on and we don't have any data yet.

            final long now = event.timestamp;

            final long then =mLastFilteredTimestampNanos;

            final float timeDeltaMS = (now -then) * 0.000001f;

            final boolean skipSample;

            if (now < then

                    || now > then +MAX_FILTER_DELTA_TIME_NANOS

                    || (x == 0 && y ==0 && z == 0)) {

                if (LOG) {

                    Slog.v(TAG, "Resettingorientation listener.");

                }

                reset();

                skipSample = true;

            } else {

                final float alpha = timeDeltaMS/ (FILTER_TIME_CONSTANT_MS + timeDeltaMS);

                x = alpha * (x -mLastFilteredX) + mLastFilte

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值