如何使用android中的传感器管理器方位角值旋转图像?

时间:2013-02-08 08:39:28

标签: android view azimuth sensormanager

 public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);

        //accel
        sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
        sensor = sensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION);



        setContentView(R.layout.sample);
    }

    ///////////////// Basics
    /**
     * Called when the application is in the background
     */
    public void onPause()
    {
        super.onPause();
        sensorManager.unregisterListener(this);

        //this is important because Android applications
        //do not close, they are in the background
        //so resources would be hogged otherwise
    }

    /**
     * Called when the application is started
     * or in the foreground again
     */
    public void onResume()
    {
        super.onResume();
        sensorManager.registerListener(this, sensor, rate);

    }

    //==================Accel=====================
    /**
     * Called when the values of the acceleration sensor changes
     * 
     * @param e Details about the change
     */
        public void onSensorChanged(SensorEvent e) 
        {
            float azimuth=e.values[0];

                    Log.i("azimuth",String.valueOf(azimuth));


        }

        /**
         * Called when accuracy of the sensor is changed
         * 
         * @param sen Which sensor's accuracy changed
         * @param acc The new accuracy degree
         */
        public void onAccuracyChanged(Sensor sen, int acc) 
        {
        }

在这里,我使用传感器管理器获得方位角值。我想知道如何使用方位角值旋转像罗盘一样的图像视图?有人能给出一些想法吗?如何在这里使用OnDraw功能? 没有在OnDraw方法中绘制圆或线?

1 个答案:

答案 0 :(得分:0)

以下是旋转图像视图的方法:

Matrix matrix=new Matrix();
imageview.setScaleType(ScaleType.MATRIX);   //required
matrix.postRotate((float) -azimuth, imageview.getDrawable().getBounds().width()/2, 
imageview.getDrawable().getBounds().height()/2);            

imageview.setImageMatrix(matrix);

对于API 11+,有一种简单的方法:

arrow.setRotation((float) -azimuth);

您可以查看此blog以查看圆圈和指南针。

希望这有帮助。