android 實現手機方向識別

來源:互聯網
上載者:User

標籤:android   方向感應器   sensor   

//自訂互動中心圖示private BitmapDescriptor mIconLocation;//自訂感應器implements SensorEventListenerprivate MyOrientationListener myOrientationListener;//把監聽的x方向的值儲存到這裡private float mCurrentX;
    //初始化表徵圖        mIconLocation = BitmapDescriptorFactory.fromResource(R.drawable.ic_launcher);        //初始化回調介面        myOrientationListener = new MyOrientationListener(getApplicationContext());        //設定回調介面        myOrientationListener.setOnOrientationListener(new OnOrientationListener() {            @Override            public void OnOrientationChanged(float x) {                // TODO Auto-generated method stub                //回調介面的值存入mCurrentX                mCurrentX=x;            }        });

在Activity的生命週期裡面實現 與 方向識別的生命週期同步

start方法裡面

    //方向識別開始        myOrientationListener.start();      

stop方法裡面

//停止方向感應器        myOrientationListener.stop();

在本機資料設定裡面

    MyLocationData data = new MyLocationData.Builder()    //設定方向    .direction(mCurrentX)//設定自訂表徵圖        MyLocationConfiguration config = new MyLocationConfiguration(com.baidu.mapapi.map.MyLocationConfiguration.LocationMode.NORMAL,                         true, mIconLocation);                mBaiduMap.setMyLocationConfigeration(config);

下面是感應器監聽的實現代碼

public class MyOrientationListener implements SensorEventListener{    //用來擷取Seosor    private SensorManager mSensorManager;    private Sensor mSensor;    private Context mcontext;       private float lastX;    private OnOrientationListener mOnOrientationListener;    public MyOrientationListener(Context context) {        super();        context=this.mcontext;    }    @SuppressWarnings("deprecation")    //開啟監聽的方法    public void start(){            mSensorManager = (SensorManager) mcontext.getSystemService(Context.SENSOR_SERVICE);            if(mSensorManager!=null){            mSensor = mSensorManager.getDefaultSensor(SensorManager.SENSOR_ORIENTATION);                    }            if(mSensor!=null){            //註冊監聽                mSensorManager.registerListener(this, mSensor, SensorManager.SENSOR_DELAY_UI);            }    }    //停止監聽的方法    public void stop(){        mSensorManager.unregisterListener(this);    }    @Override    public void onAccuracyChanged(Sensor arg0, int arg1) {    }    @Override    public void onSensorChanged(SensorEvent event) {//拿到方向感應器       if(event.sensor.getType()==Sensor.TYPE_ORIENTATION){            float x = event.values[SensorManager.DATA_X];            //如果移動超過一個精度則轉向            if(Math.abs(x-lastX)>1.0){                if(mOnOrientationListener!=null){                    mOnOrientationListener.OnOrientationChanged(x);                }            }            lastX  =x;        }           }    public void setOnOrientationListener(OnOrientationListener mOnOrientationListener) {        this.mOnOrientationListener = mOnOrientationListener;    }//定義回調介面    public interface OnOrientationListener{    //傳遞改變的監聽的x方向的值        void OnOrientationChanged(float x);    }}

android 實現手機方向識別

聯繫我們

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