我希望在我的活动中有一个代表建筑物的背景。我想绘制一个代表手机实际位置的小圆点/圆圈,但我无法弄清楚,应该怎么做。我读了一些主题,写下这个:
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SurfaceView surface = (SurfaceView) findViewById(R.id.surface);
surface.getHolder().addCallback(new SurfaceHolder.Callback() {
@Override
public void surfaceCreated(SurfaceHolder holder) {
// Do some drawing when surface is ready
Canvas canvas = holder.lockCanvas();
Paint paint = new Paint();
paint.setColor(Color.RED);
canvas.drawRect(50, 50, 200, 200, paint);
//canvas.drawColor(Color.RED);
holder.unlockCanvasAndPost(canvas);
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
});
}
}
和此:
<FrameLayout 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:background="@mipmap/map"
>
<SurfaceView
android:id="@+id/surface"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</FrameLayout>
但它不起作用,当它运行时,地图消失,只有红色矩形可见。
我的问题是:离开背景地图应该怎么做?
答案 0 :(得分:1)
为了做到这一点,你必须使SurfaceView透明。看看这个答案: https://stackoverflow.com/a/7061396/5512274
一切顺利:)