我试图实现一个 MapView,在它上面是一个 SurfaceView,它们分别工作得很好,但是当我把它们都放在 FrameLayout 中时,我无法得到
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);
}
}
}
MapActivity 中的 onCreate:
protected void onCreate(Bundle 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);
}
我总是到达
if (!sc.isValid())
并将 sc.isValid() 设为 false。