我一直在尝试添加一个可绘制的形状作为我要在地图上添加的标记的标记图标。
形状看起来像这样(res / drawable / blue_circle.xml):
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval" >
<size
android:width="15dp"
android:height="15dp" />
<solid
android:color="@color/Blue" />
</shape>
我尝试添加这样的标记:
markerOptions.icon(BitmapDescriptorFactory.fromResource(R.drawable.blue_circle));
显然我得到了NullPointer异常。
标记图标必须是位图吗? 我可以添加形状drawables作为标记图标吗? 如果是的话,我做错了什么?
答案 0 :(得分:38)
为您的标记创建一个drawable(res / drawable / map_dot_red.xml):
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval" >
<gradient
android:angle="90"
android:endColor="#f58383"
android:startColor="#ee6464" />
<stroke
android:width="1dp"
android:color="#a13939" />
</shape>
从drawable创建一个位图:
int px = getResources().getDimensionPixelSize(R.dimen.map_dot_marker_size);
mDotMarkerBitmap = Bitmap.createBitmap(px, px, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(mDotMarkerBitmap);
Drawable shape = getResources().getDrawable(R.drawable.map_dot_red);
shape.setBounds(0, 0, mDotMarkerBitmap.getWidth(), mDotMarkerBitmap.getHeight());
shape.draw(canvas);
使用位图创建标记:
Marker marker = mMap.addMarker(new MarkerOptions()
.position(point)
.anchor(.5f, .5f)
.icon(BitmapDescriptorFactory.fromBitmap(mDotMarkerBitmap)));
以dimension(res / values / dimens.xml)设置标记的大小:
<resources>
<dimen name="map_dot_marker_size">12dp</dimen>
</resources>
答案 1 :(得分:3)
如果你正在使用com.google.maps.android:android-maps-utils
库,那么这是一个答案。
使用IconGenerator
IconGenerator iconGen = new IconGenerator(context);
// Define the size you want from dimensions file
int shapeSize = getResources().getDimensionPixelSize(R.dimen.shape_size);
Drawable shapeDrawable = ResourcesCompat.getDrawable(getResources(), R.drawable.my_shape, null);
iconGen.setBackground(shapeDrawable);
// Create a view container to set the size
View view = new View(context);
view.setLayoutParams(new ViewGroup.LayoutParams(shapeSize, shapeSize));
iconGen.setContentView(view);
// Create the bitmap
Bitmap bitmap = iconGen.makeIcon();
然后像设置标记图标BitmapDescriptorFactory.fromBitmap(bitmap)
答案 2 :(得分:0)
我处理shape(xml)drawables作为标记图标的方法(基于萨克斯曼答案)。
我在地图上显示了很多对象(标记)。它们有两种绘图 - 位图和形状。我用于多个标记的相同图标。因此,我为位图描述符和可绘制类型检测添加了一些缓存。
SparseArray<BitmapDescriptor> iconCache = new SparseArray<>();
for (MyObject object : objects) {
int iconResId = object.icon;
BitmapDescriptor icon = iconCache.get(iconResId);
if (icon == null) {
Drawable drawable;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
drawable = getResources().getDrawable(iconResId, null);
} else {
drawable = getResources().getDrawable(iconResId);
}
if (drawable instanceof GradientDrawable) {
Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, bitmap.getWidth(), bitmap.getHeight());
drawable.draw(canvas);
icon = BitmapDescriptorFactory.fromBitmap(bitmap);
bitmap.recycle();
} else {
icon = BitmapDescriptorFactory.fromResource(iconResId);
}
iconCache.put(iconResId, icon);
map.addMarker(new MarkerOptions()
.icon(icon)
.position(position));
}
}
我的形状可绘制有设置大小,因此,我可以查询可绘制的位图所需的大小。
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<size android:width="12dp" android:height="12dp" />
<solid android:color="@color/transit_bus" />
</shape>