Android指南針之加速度感應器地磁感應器-android學習之旅(67)

來源:互聯網
上載者:User

標籤:android   指南針   

由於andorid不推薦用傳統的方向感應器,推薦用加速度感應器和地磁感應器來構造得到方向感應器的資料,其實主要是z軸的旋轉角度具體程式碼範例代碼如下
public class MainActivity extends Activity {    private ImageView compass;    private SensorManager sensorManager;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        compass = (ImageView) findViewById(R.id.compass);        sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);        Sensor AccelerometerSensor = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);        Sensor magneticSensor = sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);        sensorManager.registerListener(sensorEventListener,magneticSensor,SensorManager.SENSOR_DELAY_GAME);        sensorManager.registerListener(sensorEventListener,AccelerometerSensor,SensorManager.SENSOR_DELAY_GAME);    }    private SensorEventListener sensorEventListener = new SensorEventListener() {        float[] accelerometerValues = new float[3];        float[] maneticValues = new float[3];        private float lastDegree;        @Override        public void onSensorChanged(SensorEvent sensorEvent) {            if (sensorEvent.sensor.getType() == Sensor.TYPE_ACCELEROMETER){                accelerometerValues = sensorEvent.values.clone();            }else if (sensorEvent.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD){                maneticValues = sensorEvent.values.clone();            }            float[] R = new float[9];            float[] values = new float[3];            SensorManager.getRotationMatrix(R,null,accelerometerValues,maneticValues);            SensorManager.getOrientation(R,values);            float rotateDegree = -(float) Math.toDegrees(values[0]);            if (Math.abs(rotateDegree - lastDegree) > 1){                RotateAnimation animation = new RotateAnimation(lastDegree,rotateDegree, Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);                animation.setFillAfter(true);                compass.startAnimation(animation);                lastDegree = rotateDegree;            }        }        @Override        public void onAccuracyChanged(Sensor sensor, int i) {        }    };    @Override    protected void onDestroy() {        if (sensorManager != null){            sensorManager.unregisterListener(sensorEventListener);        }        super.onDestroy();    }    @Override    public boolean onCreateOptionsMenu(Menu menu) {        // Inflate the menu; this adds items to the action bar if it is present.        getMenuInflater().inflate(R.menu.menu_main, menu);        return true;    }    @Override    public boolean onOptionsItemSelected(MenuItem item) {        // Handle action bar item clicks here. The action bar will        // automatically handle clicks on the Home/Up button, so long        // as you specify a parent activity in AndroidManifest.xml.        int id = item.getItemId();        //noinspection SimplifiableIfStatement        if (id == R.id.action_settings) {            return true;        }        return super.onOptionsItemSelected(item);    }}

著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

Android指南針之加速度感應器地磁感應器-android學習之旅(67)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.