2012-10-30 53 views
0

我試圖實現一個MapView,它的頂部是一個SurfaceView,他們都很好地單獨工作,但是當我把它們都放在FrameLayout中時,我無法得到SurfaceView在MapView上,表面無效

getHolder()。getSurface()。isValid()的

爲真。

XML:

<RelativeLayout 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" > 
<FrameLayout 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:layout_alignParentRight="true" > 

    <com.google.android.maps.MapView 
     android:id="@+id/mapView" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:apiKey="0Xja4jU2XxeYRsWmblq0ccK2EweYe469rQRgWFg" 
     android:clickable="true" 
     android:enabled="true" > 
    </com.google.android.maps.MapView> 

<com.example.tricorder.BallSurfaceView 
    android:id="@+id/nixx" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    /> 

    <ImageButton 
     android:id="@+id/imageButton1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginBottom="124dp" 
     android:src="@drawable/ballz" /> 

</FrameLayout> 
</RelativeLayout> 

SurfaceView代碼:

公共類BallSurfaceView擴展SurfaceView實現Runnable {

float x, y, sensorX, sensorY, a, b, centerX, centerY; 
int color = 0; 
SurfaceHolder ourHolder; 
Thread ourThread = null; 
boolean isRunning = false; 

public BallSurfaceView(Context context) { 
    super(context); 
    ourHolder = getHolder(); 
    x = y = sensorY = sensorX = 0; 

} 

public BallSurfaceView(Context context, AttributeSet attrs, int defStyle) { 
    super(context, attrs, defStyle); 
    ourHolder = getHolder(); 
    x = y = sensorY = sensorX = 0; 
    // TODO Auto-generated constructor stub 
} 

public BallSurfaceView(Context context, AttributeSet attrs) { 
    super(context, attrs); 
    ourHolder = getHolder(); 
    x = y = sensorY = sensorX = 0; 
    // TODO Auto-generated constructor stub 
} 

public void pause() { 
    isRunning = false; 
    while (true) { 
     try { 
      ourThread.join(); 
     } catch (InterruptedException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     break; 
    } 
    ourThread = null; 
} 

public void resume() { 
    isRunning = true; 
    ourThread = new Thread(this); 
    ourThread.start(); 
} 

public void run() { 
    // TODO Auto-generated method stub 
    while (isRunning) { 
     Surface sc = ourHolder.getSurface(); 
     if (!sc.isValid()) 
      continue; 

     Canvas canvas = ourHolder.lockCanvas(); 
     //if (color == 0) 
      //canvas.drawRGB(0, 0, 0); 
    // else if (color == 1) 
      //canvas.drawRGB(127, 127, 127); 
     //else 
      //canvas.drawRGB(255, 255, 255); 
     centerX = canvas.getWidth()/2; 
     centerY = canvas.getHeight()/2; 
     x += sensorX; 
     y += sensorY; 
     a = centerX + x; 
     b = centerY + y; 

     if (x > (1.5 * centerX)) { 
      x = (float) (1.5 * centerX); 
     } 
     if (x <= (0.2 * centerX)) { 
      x = (float) (0.2 * centerX); 
     } 
     if (y > (1.5 * centerX)) { 
      y = (float) (1.5 * centerX); 
     } 
     if (y <= (0.2 * centerX)) { 
      y = (float) (0.2 * centerX); 
     } 
     // if (a > canvas.getWidth()) 
     // a = canvas.getWidth(); 
     // if (b > canvas.getHeight()) 
     // b = canvas.getHeight(); 
     // if (a < 0) 
     // a = 0; 
     // if (b < 0) 
     // b = 0; 

     canvas.drawBitmap(GraphicActivity.ball, x, y, null); 
     ourHolder.unlockCanvasAndPost(canvas); 
    } 
} 

} 

的onCreate在MapActivity:

保護無效的onCreate(捆綁savedInstanceState){

super.onCreate(savedInstanceState); 
      mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE); 
    mMag = mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD); 
    mAcc = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER); 
    mLight = mSensorManager.getDefaultSensor(Sensor.TYPE_LIGHT); 

    ball = BitmapFactory.decodeResource(getResources(), R.drawable.ballz); 
    ourSurfaceView = new BallSurfaceView(this); 
    ourSurfaceView.resume(); 
    //ourSurfaceView = (BallSurfaceView) findViewById(R.id.nixx); 
    setContentView(R.layout.activity_graphic); 

    mapView = (MapView) findViewById(R.id.mapView); 
    List mapOverlays = mapView.getOverlays(); 
    Drawable drawable = this.getResources().getDrawable(R.drawable.ballz); 
    CustomItemizedOverlay itemizedOverlay = new CustomItemizedOverlay(
      drawable, this); 

    GeoPoint point = new GeoPoint(37985339, 23716735); 
    OverlayItem overlayitem = new OverlayItem(point, "Hello", 
      "I'm in Athens, Greece!"); 

    itemizedOverlay.addOverlay(overlayitem); 
    mapOverlays.add(itemizedOverlay); 

    MapController mapController = mapView.getController(); 
    mapController.animateTo(point); 
    mapController.setZoom(6); 
} 

我總是達不到

如果

(sc.isValid()!)

並將sc.isValid()設置爲false。

回答

0

沒關係,我得到它的工作。 的問題是,我沒有在XML鏈接surfaceview到surfaceview實例如下所示:

ourSurfaceView =(BallSurfaceView)findViewById(R.id.nixx);

然後,我不需要創建我們的SurfaceView的'新'實例。