900字范文,内容丰富有趣,生活中的好帮手!
900字范文 > Android13 上层修改各类传感器数据/相机ui判断加速度传感器旋转方向

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

时间:2020-01-23 07:43:12

相关推荐

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

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

framework/base/core/java/android/hardware/SystemSensorManager.java@SuppressWarnings("unused")@Overrideprotected void dispatchSensorEvent(int handle, float[] values, int inAccuracy,long timestamp) {final Sensor sensor = mManager.mHandleToSensor.get(handle);if (sensor == null) {// sensor disconnectedreturn;}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 changesfinal 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 changedif (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.javaclass 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 valueif (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 rangewhile (orientation >= 360) {orientation -= 360;} while (orientation < 0) {orientation += 360;}+ orientation +=90;}特别的项目可以通过修改标红处理论上可以实现任何方向的适配,数值没什么规律就是多试

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。