Ich hatte das gleiche Problem, aber ich erkannte, dass auf meinem Gerät mit API 16 funktioniert es gut, aber mit API 21 stürzt es ab. Schließlich funktioniert es in beiden Geräten mit this solution. Hier ist der Code:
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private static Bitmap getBitmap(VectorDrawable vectorDrawable) {
Bitmap bitmap = Bitmap.createBitmap(vectorDrawable.getIntrinsicWidth(),
vectorDrawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
vectorDrawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
vectorDrawable.draw(canvas);
return bitmap;
}
und diese:
private static Bitmap getBitmap(Context context, int drawableId) {
Drawable drawable = ContextCompat.getDrawable(context, drawableId);
if (drawable instanceof BitmapDrawable) {
return BitmapFactory.decodeResource(context.getResources(), drawableId);
} else if (drawable instanceof VectorDrawable) {
return getBitmap((VectorDrawable) drawable);
} else {
throw new IllegalArgumentException("unsupported drawable type");
}
}
So kombiniert ich diese zwei Funktionen auf diese Weise:
private Marker addMark(LatLng latLng, String title) {
Bitmap bitmap = getBitmap(getContext(), R.drawable.ic_place_24dp);
Marker marker = googleMap.addMarker(new MarkerOptions().position(latLng)
.title(title)
.icon(BitmapDescriptorFactory.fromBitmap(bitmap))
.draggable(true));
return marker;
}
Wo R.drawable.ic_place_24dp
ist ein Vektor Vermögenswert (.xml), nicht a .png
Während dies theoretisch die Frage beantworten könnte, [wäre es vorzuziehen] (// meta.stackoverflow.com/q/8259), die e wesentliche Teile der Antwort hier und bieten den Link als Referenz. –
Danke, ich denke es ist jetzt besser :) –
Es ist viel besser jetzt. Dank dafür. Und ein Lob für die Mühe –