I am new to Android development and I have a pro开发者_如何学编程blem that I have been working on for hours with no success. I want to create a cross-hair in the center of my Google Map that stays in the center even when the map is panned. I have a .png image of the cross-hair in my drawable directory. Additionally, I have a MapView that displays the Google Map with several markers. What is the best way to approach this problem?
You will need to make a CrosshairOverlay. Haven't tested this.
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;
}
}
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;
}
}
That's what i have build for a simple CrossHairOverlay (using a Fragment and Google maps 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>
That's an example screenshot:
I took KingAlex1985's answer and made it a bit simpler. It produces a crosshair at screen center with a dot, military style
````
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_centerInParent="true"
android:foregroundGravity="center"
android:gravity="center"
android:orientation="horizontal">
<View
android:layout_width="15dp"
android:layout_height="1dp"
android:background="@android:color/black"
/>
<View
android:layout_width="5dp"
android:layout_height="1dp"
android:background="@android:color/transparent"
/>
<View
android:layout_width="2dp"
android:layout_height="2dp"
android:background="@android:color/black"
/>
<View
android:layout_width="5dp"
android:layout_height="1dp"
android:background="@android:color/transparent"
/>
<View
android:layout_width="15dp"
android:layout_height="1dp"
android:background="@android:color/black"
/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_centerInParent="true"
android:foregroundGravity="center"
android:gravity="center"
android:orientation="vertical">
<View
android:layout_width="1dp"
android:layout_height="15dp"
android:background="@android:color/black"
/>
<View
android:layout_width="1dp"
android:layout_height="11dp"
android:background="@android:color/transparent"
/>
<View
android:layout_width="1dp"
android:layout_height="15dp"
android:background="@android:color/black"
/>
</LinearLayout>
精彩评论