android高分段進階攻略(5)android指南針

來源:互聯網
上載者:User

前段時間,有一位網友發私信給我(@伍歌),問我做過磁場感應器可以做過指南針嗎?其實我第一節裡面已經說過了,磁場感應器可以做,只是演算法比較麻煩,最簡單的指南針使用方向感應器做出,但是由於工作關係,一直沒有來得及協助他,現在就寫一份簡單指南針教程吧,先貼圖:


布局檔案很簡單,就一張指南針的平面圖片。



演算法第一節裡面也說過了,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 implementsSensorEventListener {public static final String TAG = "OrientationActivity方向感應器";private TextView tv_context;private Sensor mAccelerometer;private SensorManager mSensorManager;// 記錄指南針圖片轉過的角度private float currentDegree = 0f;private ImageView image;@Overrideprotected 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);}@Overrideprotected 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:

  1. LocationListener location= new LocationListener() {
  2. @Override
  3. public void onStatusChanged(String provider, int status, Bundle extras) {
  4. if (status != LocationProvider.OUT_OF_SERVICE) {
  5. updateLocation(mLocationManager
  6. .getLastKnownLocation(mLocationProvider));
  7. } else {
  8. mLocationTextView.setText(R.string.cannot_get_location);
  9. }
  10. }
  11. @Override
  12. public void onProviderEnabled(String provider) {
  13. }
  14. @Override
  15. public void onProviderDisabled(String provider) {
  16. }
  17. @Override
  18. public void onLocationChanged(Location location) {
  19. updateLocation(location);// 更新位置
  20. }
  21. };
  22. } 這樣就可以更精確的調用地理位置,但是我在寫的過程中發現一個問題,value[0]這個值是不是從一開始就已經綁定好東南西北,否則怎麼可能一開始就指向北了?求大神解釋下。。。


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.