[英]How to add drawable in runtime properly using Mapsforge
我可以在Android上使用Mapsforge lib 0.30,並且對此感到非常滿意。 我的問題是我無法以適當的方式添加自定義繪圖。 當我添加要繪制的可繪制對象時,該可繪制對象不會顯示在正確的位置,但是會顯示在屏幕的左上角。 這是我的測試代碼的示例:
MapView mapView = new MapView(this);
mapView.setClickable(true);
mapView.setBuiltInZoomControls(true);
mapView.setEnabled(true);
mapView.setMapFile(new File("/sdcard/remob/netherlands.map"));
setContentView(mapView);
// Creating my own custom drawable
Bitmap.Config conf = Bitmap.Config.ARGB_8888;
Bitmap bmp = Bitmap.createBitmap(60, 60, conf);
Drawable drawable = new BitmapDrawable(getResources(), bmp) {
@Override
public void draw(Canvas canvas) {
super.draw(canvas);
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setColor(Color.BLUE);
paint.setStyle(Style.FILL);
canvas.drawCircle(30, 30, 15, paint);
}
};
// create an ItemizedOverlay with the default marker
ArrayItemizedOverlay itemizedOverlay = new ArrayItemizedOverlay(drawable);
// create a GeoPoint with the latitude and longitude coordinates
GeoPoint geoPoint = new GeoPoint(51.92434, 4.47769);
// create an OverlayItem with title and description
OverlayItem item = new OverlayItem(geoPoint, "MyPoint", "This is my own point.");
item.setMarker(ItemizedOverlay.boundCenter(drawable));
// add the OverlayItem to the ArrayItemizedOverlay
itemizedOverlay.addItem(item);
// add the ArrayItemizedOverlay to the MapView
mapView.getOverlays().add(itemizedOverlay);
所以有人可以幫助我以正確的方式做嗎? 簡而言之,我想在運行時中創建一個drawable並將其放置在正確的位置。 僅從資源添加可繪制對象不是問題,這很好,但是從運行時開始,可繪制對象位置不正確。 謝謝。
您需要在其上調用Marker.boundCenter(...)。
我用這樣的一行:
ef_icon = Marker.boundCenter(getResources().getDrawable(R.drawable.ef_icon));
我正在Trunk(看起來像0.3.1-snapshot到Maven)。
如果您的Mapsforge版本缺少該方法,則完整方法為:
public static Drawable boundCenter(Drawable drawable) {
int intrinsicWidth = drawable.getIntrinsicWidth();
int intrinsicHeight = drawable.getIntrinsicHeight();
drawable.setBounds(intrinsicWidth / -2, intrinsicHeight / -2, intrinsicWidth / 2, intrinsicHeight / 2);
return drawable;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.