我是Android开发的新手,我有一个问题,我已经工作了好几个小时而没有成功。我想在我的谷歌地图中心创建一个十字准线,即使在地图被淘汰时也会留在中心。我的drawable目录中有一个十字形的.png图像。另外,我有一个MapView,它显示带有几个标记的Google Map。解决这个问题的最佳方法是什么?
答案 0 :(得分:3)
您需要制作CrosshairOverlay。没试过这个。
public class CrosshairOverlay extends Overlay {
public boolean draw(Canvas canvas, MapView mapView, boolean shadow, long when) {
Projection projection = mapView.getProjection();
Point center = projection.toPixels(mapView.getMapCenter(), null);
// Customize appearance, should be a fields.
Paint p = new Paint(Paint.ANTI_ALIAS_FLAG);
p.setColor(0xFF000000);
p.setStyle(Style.STROKE);
p.setStrokeWidth(2.0f);
int innerRadius = 10;
int outerRadius = 20;
canvas.drawCircle(center.x, center.y, innerRadius, p);
canvas.drawCircle(center.x, center.y, outerRadius, p);
return true;
}
}
答案 1 :(得分:2)
public class CrossHairsOverlay extends Overlay {
public boolean draw(Canvas canvas, MapView mapView, boolean shadow, long when) {
super.draw(canvas, mapView, shadow);
GeoPoint centerGp = mapView.getMapCenter();
Projection projection = mapView.getProjection();
Point centerPoint = projection.toPixels(centerGp, null);
Paint p = new Paint();
Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.crosshairs_dial);
canvas.drawBitmap(bmp, centerPoint.x, centerPoint.y, p);
return true;
}
}
答案 2 :(得分:1)
这就是我为简单的CrossHairOverlay构建的(使用片段和谷歌地图API2 for Android):
<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">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center">
<TextView
android:id="@+id/crosshair_horizontal_line"
android:layout_width="fill_parent"
android:layout_height="1dp"
android:padding="1dp"
android:background="@color/black"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_gravity="center">
<TextView
android:id="@+id/crosshair_vertical_line"
android:layout_width="1dp"
android:layout_height="fill_parent"
android:padding="2dp"
android:background="@color/black"/>
</LinearLayout>
这是一个示例屏幕截图:
答案 3 :(得分:0)
我采用了KingAlex1985的答案并使其更简单一些。它在屏幕中心产生一个十字准线,带有一个点,军事风格
````
{{1}}