通过将布局转换为可绘制对象,我能够将布局添加为地图上的标记。这是取自this tutorial 的bitmapFromView 函数:
public static Bitmap bitmapFromView(View layout, int width, int height, Context context) {
// Create a new bitmap and a new canvas using that bitmap
Bitmap bmp = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bmp);
layout.setDrawingCacheEnabled(true);
// Supply measurements
layout.measure(View.MeasureSpec.makeMeasureSpec(canvas.getWidth(), View.MeasureSpec.EXACTLY), View.MeasureSpec.makeMeasureSpec(canvas.getHeight(), View.MeasureSpec.EXACTLY));
// Apply the measures so the layout would resize before drawing.
layout.layout(0, 0, layout.getMeasuredWidth(), layout.getMeasuredHeight());
// and now the bmp object will actually contain the requested layout
canvas.drawBitmap(layout.getDrawingCache(), 0, 0, new Paint());
return bmp;
}
我使用该方法将位图添加为这样的标记:
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.MY_LAYOUT, null);
Bitmap markerBitmap = bitmapFromView(layout, MY_WIDTH, MY_HEIGHT, context);
MarkerOptions markerOptions = new MarkerOptions()
.title(MY_TITLE)
.position(MY_POSITION)
.icon(BitmapDescriptorFactory.fromBitmap(markerBitmap));
map.addMarker(markerOptions);