Android13 上层修改各类传感器数据/相机ui判断加速度传感器旋转方向

这些通常不需要上层修改,驱动调完就是好的,所以能不改就不改

framework/base/core/java/android/hardware/SystemSensorManager.java
 @SuppressWarnings("unused")
        @Override
        protected void dispatchSensorEvent(int handle, float[] values, int inAccuracy,
                long timestamp) {
            final Sensor sensor = mManager.mHandleToSensor.get(handle);
            if (sensor == null) {
                // sensor disconnected
                return;
            }

            SensorEvent t = null;
            synchronized (mSensorsEvents) {
                t = mSensorsEvents.get(handle);
            }

            if (t == null) {
                // This may happen if the client has unregistered and there are pending events in
                // the queue waiting to be delivered. Ignore.
                return;
            }
            // Copy from the values array.
            System.arraycopy(values, 0, t.values, 0, t.values.length);
            t.timestamp = timestamp;
            t.accuracy = inAccuracy;
            t.sensor = sensor;

            // call onAccuracyChanged() only if the value changes
            final int accuracy = mSensorAccuracies.get(handle);
            if (t.accuracy >= 0 && accuracy != t.accuracy) {
                mSensorAccuracies.put(handle, t.accuracy);
                mListener.onAccuracyChanged(t.sensor, t.accuracy);
            }

            // Indicate if the discontinuity count changed
            if (t.sensor.getType() == Sensor.TYPE_HEAD_TRACKER) {
                final int lastCount = mSensorDiscontinuityCounts.get(handle);
                final int curCount = Float.floatToIntBits(values[6]);
                if (lastCount >= 0 && lastCount != curCount) {
                    mSensorDiscontinuityCounts.put(handle, curCount);
                    t.firstEventAfterDiscontinuity = true;
                }
            }
            if (t.sensor.getType()==Sensor.TYPE_ACCELEROMETER){
                float x=t.values[0];
                float y=t.values[1];
                t.values[0]=y;
                t.values[1]=-x;
            }
            
            //下面是新加的
            if (t.sensor.getType()==Sensor.TYPE_DEVICE_ORIENTATION){
                float orientation = t.values[0];
                t.values[0]=(orientation+1) % 4;
            }
            if (t.sensor.getType() == Sensor.TYPE_GYROSCOPE) {
                float aa = t.values[0];
                float bb = t.values[1];
                switch (getScreenOrientation(mManager.mContext)) {
                    case 0:
                        t.values[0] = bb;
                        t.values[1] = -aa;
                        break;
                    case 1:
                        break;
                    case 2:
                        t.values[0] = -bb;
                        t.values[1] = aa;
                        break;
                    case 3:
                        t.values[0] = -aa;
                        t.values[1] = -bb;
                        break;
                    default:
                        break;
                }
            }
            mListener.onSensorChanged(t);
        }

     t.values[0]和t.values[1] 传感器传到上层的xy轴源数据通过判断t.sensor.getType()区分sensor类型(Sensor.TYPE_DEVICE_ORIENTATION修改屏幕方向,Sensor.TYPE_GYROSCOPE修改陀螺仪方向)

通过一些计算适配当前项目
相机ui旋转方向
framework/base/core/java/android/view/OrientationEventListener.java
 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 = -180 - (int)Math.round(angle);
   +             orientation = 90 - (int)Math.round(angle);
                // normalize to 0 - 359 range
                while (orientation >= 360) {
                    orientation -= 360;
                } 
                while (orientation < 0) {
                    orientation += 360;
                }
    +            orientation +=90;
            }
           

特别的项目可以通过修改标红处理论上可以实现任何方向的适配,数值没什么规律就是多试
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值