2013-02-08 23 views
0
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) 
     { 
     } 

在這裏,我正在使用傳感器管理器獲取方位角值。我想知道如何使用方位角值來旋轉像羅盤的imageview?任何人都可以提供一些想法嗎?如何在這裏使用OnDraw功能? 沒有在OnDraw方法中繪製圓或線?如何使用Android中的傳感器管理器方位角值旋轉圖像?

回答

0

下面是可以旋轉的ImageView的:

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爲圓和直線指南針。

希望這會有所幫助。