前段時間,有一位網友發私信給我(@伍歌),問我做過磁場傳感器可以做過指南針嗎?其實我第一節裡面已經說過瞭,磁場傳感器可以做,隻是算法比較麻煩,最簡單的指南針使用方向傳感器做出,但是由於工作關系,一直沒有來得及幫助他,現在就寫一份簡單指南針教程吧,先貼圖:
佈局文件很簡單,就一張指南針的平面圖片。
算法第一節裡面也說過瞭,values[0]:該值表示方位,也就是手機繞著Z軸旋轉的角度。 0表示北(North);90表示東(East);180表示南(South);270表示西(West)。如果values[0]的值正好是這4個值,並且手機是水平放置,表示手機的正前方就是這4個方向。可以利用這個特性來實現電子羅盤。如果還有什麼疑問請看第一節內容。
具體方法代碼
public void onSensorChanged(SensorEvent event) { // 如果真機上觸發event的傳感器類型為水平傳感器類型 if (event.sensor.getType() == Sensor.TYPE_ORIENTATION) { // 獲取繞Z軸旋轉的角度 float degree = event.values[0]; // 創建旋轉動畫(反向轉過degree度) RotateAnimation ra = new RotateAnimation(currentDegree, -degree, Animation.RELATIVE_TO_SELF, 0.5f,Animation.RELATIVE_TO_SELF, 0.5f); // 設置動畫的持續時間 ra.setDuration(200); // 設置動畫結束後的保留狀態 ra.setFillAfter(true); // 啟動動畫 image.startAnimation(ra); currentDegree = -degree; } }
思路就是獲取瞭values[0],根據values[0]的值去旋轉圖片。所有代碼如下:
public class OrientationActivity extends Activity implements SensorEventListener { public static final String TAG = "OrientationActivity方向傳感器"; private TextView tv_context; private Sensor mAccelerometer; private SensorManager mSensorManager; // 記錄指南針圖片轉過的角度 private float currentDegree = 0f; private ImageView image; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main_orien); infoViews();// 初始化控件 } private void infoViews() { // btn = (Button) findViewById(R.id.btn_sensor); tv_context = (TextView) findViewById(R.id.tv_context); tv_context.setText("指南針"); mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE); mAccelerometer = mSensorManager .getDefaultSensor(Sensor.TYPE_ORIENTATION); image = (ImageView) findViewById(R.id.main_iv); } @Override protected void onResume() { if (mAccelerometer != null) { mSensorManager.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_NORMAL); Toast.makeText(getApplicationContext(), "此設備有方向傳感器", 0).show(); } else { Toast.makeText(getApplicationContext(), "此設備沒有方向傳感器", 0).show(); } super.onResume(); } protected void onPause() { super.onPause(); mSensorManager.unregisterListener(this); } public void onAccuracyChanged(Sensor sensor, int accuracy) { } public void onSensorChanged(SensorEvent event) { // 如果真機上觸發event的傳感器類型為水平傳感器類型 if (event.sensor.getType() == Sensor.TYPE_ORIENTATION) { // 獲取繞Z軸旋轉的角度 float degree = event.values[0]; // 創建旋轉動畫(反向轉過degree度) RotateAnimation ra = new RotateAnimation(currentDegree, -degree, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); // 設置動畫的持續時間 ra.setDuration(200); // 設置動畫結束後的保留狀態 ra.setFillAfter(true); // 啟動動畫 image.startAnimation(ra); currentDegree = -degree; } } }
很簡單,但是如果我們需要優化的話,就需要調用Criteria這個類去加載location信息:
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);//設置為最大精度
criteria.setAltitudeRequired(false);//不要求海拔信息
criteria.setBearingRequired(false);//不要求方位信息
criteria.setCostAllowed(true);//是否允許付費
criteria.setPowerRequirement(Criteria.POWER_LOW);//對電量的要求
location = locationManager.getLastKnownLocation(locationManager.getBestProvider(criteria, true));
然後去寫location:
- LocationListener location= new LocationListener() {
- @Override
- public void onStatusChanged(String provider, int status, Bundle extras) {
- if (status != LocationProvider.OUT_OF_SERVICE) {
- updateLocation(mLocationManager
- .getLastKnownLocation(mLocationProvider));
- } else {
- mLocationTextView.setText(R.string.cannot_get_location);
- }
- }
- @Override
- public void onProviderEnabled(String provider) {
- }
- @Override
- public void onProviderDisabled(String provider) {
- }
- @Override
- public void onLocationChanged(Location location) {
- updateLocation(location);// 更新位置
- }
- };
-
}
這樣就可以更精確的調用地理位置,但是我在寫的過程中發現一個問題,value[0]這個值是不是從一開始就已經綁定好東南西北,否則怎麼可能一開始就指向北瞭?求大神解釋下。。。