如何使用Android上的Accelerometer旋转标记

时间:2017-02-04 15:52:02

标签: android google-maps rotation

我试图在设备像谷歌地图一样旋转时旋转标记。现在,我正在使用marker.roatate(bearing);在旅行时旋转标记。但是现在我想在用户旋转设备时旋转标记。我从this link得到了一个样本。并做了一些修改。但没有运气。请让我知道。

public class SensorExample extends Activity implements SensorEventListener {

    Float azimut=0.0f;
    //CustomDrawableView mCustomDrawableView;
    private SensorManager mSensorManager;
    Sensor accelerometer;
    Sensor magnetometer;
    ImageView mNavigator;

    public class CustomDrawableView extends View {
        Paint paint = new Paint();
        public CustomDrawableView(Context context) {
            super(context);
            //color
            paint.setColor(0xff00ff00);
            //style
            paint.setStyle(Paint.Style.STROKE);
            //stroke width
            paint.setStrokeWidth(2);
            //antiAlias
            paint.setAntiAlias(true);
            //text size
            paint.setTextSize(30);
        };

        protected void onDraw(Canvas canvas) {
            //declare Local Variables
            int width = getWidth();
            int height = getHeight();
            int centerx = width/2;
            int centery = height/2;
            // Rotate the canvas with the azimut
            if (azimut != null)
                //Preconcat the current matrix with the specified rotation.
                canvas.rotate(-azimut*360/(2*3.14159f), centerx, centery);
            //set color
            paint.setColor(0xff0000ff);
            //draw two lines
            canvas.drawLine(centerx, -1000, centerx, +1000, paint);
            canvas.drawLine(-1000, centery, 1000, centery, paint);
            //E,W,N,S directions
            canvas.drawText("N", centerx+15, centery-220, paint);
            canvas.drawText("S", centerx-30, centery+225, paint);
            canvas.drawText("E", centerx+215, centery-20, paint);
            canvas.drawText("W", centerx-220, centery+35, paint);
        }
    }



    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //mCustomDrawableView = new CustomDrawableView(this);
        //setContentView(mCustomDrawableView);
        //create instance of sensor manager and get system service to interact with Sensor
        setContentView(R.layout.sensor_layout);
        mNavigator = (ImageView)findViewById(R.id.ic_navigator);
        mSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
        accelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
        magnetometer = mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        mSensorManager.registerListener(this, mSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION),
                SensorManager.SENSOR_DELAY_GAME);
    }

    protected void onResume() {
        super.onResume();
        // Register the sensor listeners
        mSensorManager.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_UI);
        mSensorManager.registerListener(this, magnetometer, SensorManager.SENSOR_DELAY_UI);
        mSensorManager.registerListener(this, mSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION),
                SensorManager.SENSOR_DELAY_GAME);
    }

    protected void onPause() {
        super.onPause();
        // unRegister the sensor listeners
        mSensorManager.unregisterListener(this);
    }

    public void onAccuracyChanged(Sensor sensor, int accuracy) {  }

    float[] mGravity;
    float[] mGeomagnetic;
    public void onSensorChanged(SensorEvent event) {
        if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER)
            mGravity = event.values;
        if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD)
            mGeomagnetic = event.values;
        if (mGravity != null && mGeomagnetic != null) {
            float R[] = new float[9];
            float I[] = new float[9];
            boolean success = SensorManager.getRotationMatrix(R, I, mGravity, mGeomagnetic);
            if (success) {
                float orientation[] = new float[3];
                SensorManager.getOrientation(R, orientation);
                azimut = orientation[0];
            }
        }
        //mCustomDrawableView.invalidate();
        rotateImage(azimut);
    }

     private void rotateImage(float azimuth){
        Matrix matrix=new Matrix();
        mNavigator.setScaleType(ImageView.ScaleType.MATRIX);
        matrix.postRotate((float) -azimuth, mNavigator.getDrawable().getBounds().width()/2,
                mNavigator.getDrawable().getBounds().height()/2);
         mNavigator.setImageMatrix(matrix);
    }

    @Override
    protected void onStop()
    {
        // Unregister the listener
        mSensorManager.unregisterListener(this);
        super.onStop();
    }

0 个答案:

没有答案