We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Android中的方向传感器可以准确的判断出手机在各个方向的旋转角度,利用这些角度就可以编写出像指南针、地平仪等有用的工具。下面,我来制作一个简单指南针。先附上代码:
public class MainActivity extends AppCompatActivity implements SensorEventListener{
private SensorManager sensorManager; private ImageView compass_img; private float lastRotateDegree; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); compass_img = (ImageView) findViewById(R.id.compass_img); sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE); //加速度感应器 Sensor magneticSensor = sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD); //地磁感应器 Sensor accelerometerSensor = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER); sensorManager.registerListener(this, magneticSensor, SensorManager.SENSOR_DELAY_GAME); sensorManager.registerListener(this, accelerometerSensor, SensorManager.SENSOR_DELAY_GAME); } @Override protected void onDestroy() { super.onDestroy(); if (sensorManager != null) { sensorManager.unregisterListener(this); } } float[] accelerometerValues = new float[3]; float[] magneticValues = new float[3]; @Override public void onSensorChanged(SensorEvent event) { // 判断当前是加速度感应器还是地磁感应器 if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) { //赋值调用clone方法 accelerometerValues = event.values.clone(); } else if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD) { //赋值调用clone方法 magneticValues = event.values.clone(); } float[] R = new float[9]; float[] values = new float[3]; SensorManager.getRotationMatrix(R,null,accelerometerValues,magneticValues); sensorManager.getOrientation(R, values); Log.d("Main","values[0] :"+Math.toDegrees(values[0])); //values[0]的取值范围是-180到180度。 //+-180表示正南方向,0度表示正北,-90表示正西,+90表示正东 //将计算出的旋转角度取反,用于旋转指南针背景图 float rotateDegree = -(float) Math.toDegrees(values[0]); if (Math.abs(rotateDegree - lastRotateDegree) > 1) { RotateAnimation animation = new RotateAnimation(lastRotateDegree,rotateDegree, Animation.RELATIVE_TO_SELF,0.5f, Animation.RELATIVE_TO_SELF,0.5f); animation.setFillAfter(true); compass_img.startAnimation(animation); lastRotateDegree = rotateDegree; } } @Override public void onAccuracyChanged(Sensor sensor, int accuracy) { }
}
<ImageView android:id="@+id/compass_img" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:background="@drawable/compass"/>
在onCreate方法中分别获取了加速度传感器和地磁传感器实例,并给他们都注册了监听器。然后在onSensorChanged()方法中进行判断,如果当前是加速度传感器,就将values数组赋给accelerometerValues数组,如果当前是地磁传感器,就将values数组赋给magneticValues数组。在赋值的时候一定要调用values的clone()方法,不然accelerometerValues和magneticValues将指向同一块引用。 接下来,分别创建一个长度为9的R数组和一个长度为3的values数组,然后调用getRotationMatrix()方法给R数组赋值,再调用getOrientation()方法为values数组赋值,这是values数组中含有手机在所有方向上旋转的弧度了。(可以将弧度转换为度) 准备一张指南针的图片,在回调方法中利用RotateAnimation和当前旋转过的角度值values[0],可以将图片旋转。 这样就完成了一个简易的指南针。
The text was updated successfully, but these errors were encountered:
No branches or pull requests
方向传感器(制作指南针)
Android中的方向传感器可以准确的判断出手机在各个方向的旋转角度,利用这些角度就可以编写出像指南针、地平仪等有用的工具。下面,我来制作一个简单指南针。先附上代码:
Activity:
public class MainActivity extends AppCompatActivity implements SensorEventListener{
}
XML:
在onCreate方法中分别获取了加速度传感器和地磁传感器实例,并给他们都注册了监听器。然后在onSensorChanged()方法中进行判断,如果当前是加速度传感器,就将values数组赋给accelerometerValues数组,如果当前是地磁传感器,就将values数组赋给magneticValues数组。在赋值的时候一定要调用values的clone()方法,不然accelerometerValues和magneticValues将指向同一块引用。
接下来,分别创建一个长度为9的R数组和一个长度为3的values数组,然后调用getRotationMatrix()方法给R数组赋值,再调用getOrientation()方法为values数组赋值,这是values数组中含有手机在所有方向上旋转的弧度了。(可以将弧度转换为度)
准备一张指南针的图片,在回调方法中利用RotateAnimation和当前旋转过的角度值values[0],可以将图片旋转。
这样就完成了一个简易的指南针。
The text was updated successfully, but these errors were encountered: