Android 地图视图顶部的SurfaceView,曲面无效

Android 地图视图顶部的SurfaceView,曲面无效,android,android-layout,Android,Android Layout,我试图实现一个MapView,在上面是一个SurfaceView,它们各自都很好地工作,但是当我把它们都放在一个FrameLayout中时,我无法得到 getHolder().getSurface().isValid() 没错 XML: 我总是伸手去拿 如果(!sc.isValid()) 然后将sc.isValid()设为false。没关系,我让它工作了。 问题是我没有将XML中的surfaceview链接到surfaceview实例,如下所示: ourSurfaceView=(BallSurf

我试图实现一个MapView,在上面是一个SurfaceView,它们各自都很好地工作,但是当我把它们都放在一个FrameLayout中时,我无法得到

getHolder().getSurface().isValid()

没错

XML:

我总是伸手去拿

如果(!sc.isValid())


然后将sc.isValid()设为false。

没关系,我让它工作了。 问题是我没有将XML中的surfaceview链接到surfaceview实例,如下所示:

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

然后,我不需要创建我们的SurfaceView的“新”实例

<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>
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);
    }
}

}
    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);
}