@Lukas Novak 答案没有显示任何内容,因为您还必须在Drawable
.
这适用于任何可绘制对象。这是一个完整的工作示例:
public void drawMarker() {
Drawable circleDrawable = getResources().getDrawable(R.drawable.circle_shape);
BitmapDescriptor markerIcon = getMarkerIconFromDrawable(circleDrawable);
googleMap.addMarker(new MarkerOptions()
.position(new LatLng(41.906991, 12.453360))
.title("My Marker")
.icon(markerIcon)
);
}
private BitmapDescriptor getMarkerIconFromDrawable(Drawable drawable) {
Canvas canvas = new Canvas();
Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
canvas.setBitmap(bitmap);
drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
drawable.draw(canvas);
return BitmapDescriptorFactory.fromBitmap(bitmap);
}
circle_shape.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval">
<size android:width="20dp" android:height="20dp"/>
<solid android:color="#ff00ff"/>
</shape>