2013-06-04 19 views
1

在我的應用程序中,我有一個基本的指南針,它在「compassView」類中呈現。如果我在我的顯示指南針活動中設置contentView,如下所示:compassView = new CompassView(this); setContentView(compassView);它工作正常,但指向北,但如果將內容視圖設置爲setContentView(activity_display_compass),它將渲染指南針和我可能具有的任何文字瀏覽,但指南針是靜態的;即針不移動。我對這種事情造成了損失。任何指導將不勝感激?Android:自定義視圖是靜態的,它應該對方向作出反應

XML

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@string/hello_world"/> 

    <com.example.gpsfinder.CompassView 
     android:id="@+id/compassView" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"/> 

</LinearLayout> 

顯示指南針活動

public class DisplayCompass extends Activity { 

     private static SensorManager sensorService; 
     private CompassView compassView; 
     private Sensor sensorAccelerometer; 
     private Sensor sensorMagnetometer; 

     private float [] lastAccelerometer = new float[3]; 
     private float [] lastMagnetometer = new float[3]; 
     private boolean lastAccelerometerSet = false; 
     private boolean lastMagnetometerSet = false; 

     private float[] rotation = new float[9]; 
     private float[] orientation = new float[3]; 


    /** Called when the activity is first created. */ 

     @Override 
     public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     compassView = new CompassView(this); 
     setContentView(compassView); 

     sensorService = (SensorManager) getSystemService(Context.SENSOR_SERVICE); 
     sensorAccelerometer = sensorService.getDefaultSensor(Sensor.TYPE_ACCELEROMETER); 
     sensorMagnetometer = sensorService.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD); 
     //sensor = sensorService.getDefaultSensor(Sensor.TYPE_ORIENTATION); 

     if (sensorAccelerometer != null && sensorMagnetometer != null) { 
      sensorService.registerListener(mySensorEventListener, sensorAccelerometer, 
       SensorManager.SENSOR_DELAY_UI); 
      sensorService.registerListener(mySensorEventListener, sensorMagnetometer, 
        SensorManager.SENSOR_DELAY_UI); 
      Log.i("Compass MainActivity", "Registerered for ORIENTATION Sensor"); 

     } else { 
      Log.e("Compass MainActivity", "Registerered for ORIENTATION Sensor"); 
      Toast.makeText(this, "ORIENTATION Sensor not found", 
       Toast.LENGTH_LONG).show(); 
      finish(); 
     } 
     } 

     private SensorEventListener mySensorEventListener = new SensorEventListener() { 


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


     public void onSensorChanged(SensorEvent event) { 
      if(event.sensor == sensorAccelerometer){ 
       System.arraycopy(event.values,0,lastAccelerometer, 0,event.values.length); 
       lastAccelerometerSet= true; 
      } 
      else if (event.sensor == sensorMagnetometer){ 
       System.arraycopy(event.values,0,lastMagnetometer, 0,event.values.length); 
       lastMagnetometerSet = true; 
      } 
      if(lastAccelerometerSet && lastMagnetometerSet) 
       SensorManager.getRotationMatrix(rotation,null,lastAccelerometer ,lastMagnetometer); 
       SensorManager.getOrientation(rotation,orientation); 
      // angle between the magnetic north direction 
      // 0=North, 90=East, 180=South, 270=West 
      double azimuth = Math.toDegrees(orientation[0]); 
      compassView.updateDirection(azimuth); 
     } 
     }; 

     @Override 
     protected void onDestroy() { 
     super.onDestroy(); 
      sensorService.unregisterListener(mySensorEventListener); 

     } 
} 

指南針查看

public class CompassView extends View{ 

    private Paint paint; 
     private double position = 0; 

     public CompassView(Context context) { 
     super(context, null); 
     init(); 
     } 

     public CompassView(Context context, AttributeSet attrs) { 
      super(context,attrs, 0); 
      init(); 
     } 

     public CompassView(Context context, AttributeSet attrs,int defStyle) { 
      super(context,attrs, defStyle); 
      init(); 
     } 



     private void init() { 
     paint = new Paint(); 
     paint.setAntiAlias(true); 
     paint.setStrokeWidth(5); 
     paint.setTextSize(25); 
     paint.setStyle(Paint.Style.STROKE); 
     paint.setColor(Color.RED); 
     } 

     @Override 
     protected void onDraw(Canvas canvas) { 
     int xPoint = getMeasuredWidth()/2; 
     int yPoint = getMeasuredHeight()/2; 

     float radius = (float) (Math.max(xPoint, yPoint) * 0.6); 
     canvas.drawCircle(xPoint, yPoint, radius, paint); 
     canvas.drawRect(0, 0, getMeasuredWidth(), getMeasuredHeight(), paint); 

     // 3.143 is a good approximation for the circle 
     canvas.drawLine(xPoint, 
      yPoint, 
      (float) (xPoint + radius 
       * Math.sin((double) (-position)/180 * 3.143)), 
      (float) (yPoint - radius 
       * Math.cos((double) (-position)/180 * 3.143)), paint); 

     canvas.drawText(String.valueOf(position), xPoint, yPoint, paint); 
     } 

     public void updateDirection(double azimuth) { 
     this.position = azimuth; 
     invalidate(); 
     } 

    } 

回答

2

你的問題只是,當你第一編碼的應用程序,你在編程方式創建CompassView本:

compassView = new CompassView(this); 
    setContentView(compassView); 

在改變使用XML佈局,即第一行是大概刪除。但是,您仍然需要分配您的compassView成員變量,因爲您在SensorEventListeneronSensorChanged()方法中使用它。你得到一個NullPointerException(你可以通過看LogCat看到)。

因此,在你onCreate()方法,設置內容視圖後,記得要做到這一點:再次

setContentView(R.layout.activity_display_compass); 
compassView = (CompassView)findViewById(R.id.compassView); // <- missing?! 
+0

感謝內特 – Calgar99

相關問題