思路:
* 1.获取传感器管理对象SensorManager* 2.获取指定类型的传感器
* 3.注册监听,通过实时监听即可获取传感器传回来的数据
效果:
代码:
package com.fe.statuslayout.statuspage; import android.hardware.Sensor; import android.hardware.SensorEvent; import android.hardware.SensorEventListener; import android.hardware.SensorManager; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v7.app.AppCompatActivity; import android.util.Log; import android.view.View; import android.view.animation.Animation; import android.view.animation.RotateAnimation; import android.widget.Button; import android.widget.ImageView; import com.fe.statuslayout.R; /** * Created by Administrator on 2017/3/14 0014. * 安卓传感器SensorManager实现简单指南针 * 思路: * 1.获取传感器管理对象SensorManager * 2.获取指定类型的传感器 * 3.注册监听,通过实时监听即可获取传感器传回来的数据 */ public class SensorOrientation extends AppCompatActivity implements View.OnClickListener { private Button start; private ImageView south_background; private ImageView south_campass; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.sensororientation); initView(); } private void initView() { start = (Button) findViewById(R.id.start); south_background = (ImageView) findViewById(R.id.south_background); south_campass = (ImageView) findViewById(R.id.south_campass); start.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.start: openOrientationOld(); break; } } private SensorManager sensorManagerOrientationOld; private SensorEventListener listenerOrientationOld; //记录当前的角度 float currentDegree; /** * 方向 */ private void openOrientationOld() { //1.获取传感器管理对象SensorManager sensorManagerOrientationOld = (SensorManager) getSystemService(SENSOR_SERVICE); //2.获取指定类型的传感器 Sensor sensorOrientation = sensorManagerOrientationOld.getDefaultSensor(Sensor.TYPE_ORIENTATION); //3.注册监听,通过实时监听即可获取传感器传回来的数据 listenerOrientationOld = new SensorEventListener() { @Override public void onSensorChanged(SensorEvent event) { //绕Z轴旋转的角度,0度代表为正北 //z是指向地心的方角,其角度是表指南针的方向,其中180表示正南方 float zValue = Math.abs(event.values[0]); float xValue = Math.abs(event.values[2]); float yValue = Math.abs(event.values[1]); Log.d("gsonUtils", "xValue=" + xValue + "yValue=" + yValue + "zValue=" + zValue); //开始旋转 RotateAnimation animation = new RotateAnimation (currentDegree, zValue, Animation.RELATIVE_TO_SELF, 0.5f, Animation. RELATIVE_TO_SELF, 0.5f); //让动画停留在在结束的位置 animation.setFillAfter(true); //记录上一次的角度 currentDegree = zValue; south_campass.startAnimation(animation); } @Override public void onAccuracyChanged(Sensor sensor, int accuracy) { } }; //由于方向传感器的精确度要求通常都比较高,使用的是 SENSOR_DELAY_GAME sensorManagerOrientationOld.registerListener(listenerOrientationOld, sensorOrientation, SensorManager.SENSOR_DELAY_GAME); } //取消注册 @Override protected void onPause() { super.onPause(); sensorManagerOrientationOld.unregisterListener(listenerOrientationOld); } //取消注册 @Override protected void onStop() { super.onStop(); sensorManagerOrientationOld.unregisterListener(listenerOrientationOld); } }
布局:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <Button android:id="@+id/start" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="指南针"/> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <ImageView android:id="@+id/south_background" android:layout_width="match_parent" android:layout_height="wrap_content" android:src="@drawable/south_background"/> <ImageView android:id="@+id/south_campass" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_centerInParent="true" android:src="@drawable/south_campass"/> </RelativeLayout> </LinearLayout>
图片
south_background.png
south_campass.png
。。。