[英]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.