VectorDrawable与GoogleMap BitmapDescriptor

我有谷歌地图BitmapDescriptor问题,同时使用VectorDrawable ,API 5.0 +创buildMarkerOptions图标

用于创build的方法:

 @NonNull private BitmapDescriptor getBitmapDescriptor(int id) { return BitmapDescriptorFactory.fromResource(id); } 

id参数包含png可绘制时,一切都很好,但是如果我用xml中定义的VectorDrawable尝试它,当googleMap.addMarker(marker)BitmapDescriptor不为null)时,应用程序总是崩溃。

 11-05 10:15:05.213 14536-14536/xxx.xxxxx.app E/AndroidRuntime: FATAL EXCEPTION: main Process: xxx.xxxxx.app, PID: 14536 java.lang.NullPointerException at com.google.aaae.a(Unknown Source) at com.google.maps.api.android.lib6.d.dn.<init>(Unknown Source) at com.google.maps.api.android.lib6.d.dm.a(Unknown Source) at com.google.maps.api.android.lib6.d.ag.<init>(Unknown Source) at com.google.maps.api.android.lib6.d.eu.a(Unknown Source) at com.google.android.gms.maps.internal.j.onTransact(SourceFile:167) at android.os.Binder.transact(Binder.java:380) at com.google.android.gms.maps.internal.IGoogleMapDelegate$zza$zza.addMarker(Unknown Source) at com.google.android.gms.maps.GoogleMap.addMarker(Unknown Source) at xxx.xxxxx.app.ui.details.DetailActivity.lambda$initGoogleMaps$23(DetailActivity.java:387) at xxx.xxxxx.app.ui.details.DetailActivity.access$lambda$10(DetailActivity.java) at xxx.xxxxx.app.ui.details.DetailActivity$$Lambda$13.onMapReady(Unknown Source) at com.google.android.gms.maps.SupportMapFragment$zza$1.zza(Unknown Source) at com.google.android.gms.maps.internal.zzl$zza.onTransact(Unknown Source) at android.os.Binder.transact(Binder.java:380) at com.google.android.gms.maps.internal.av.a(SourceFile:82) at com.google.maps.api.android.lib6.d.fa.run(Unknown Source) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5221) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694) 

不要紧,我如何检索绘制,尝试使用BitmapFactory.fromResources和以后的BitmapDescritpionFactory.fromBitmap创build位图,但结果是相同的。 它只是不会与vector绘制工作。 尝试了不同的载体,但似乎载体的复杂性不是问题在这里。

有谁知道如何解决这个崩溃?

@编辑

这似乎是问题不是与BitmapDescriptior本身,而是加载VectorDrawable这是返回不正确的位图。 然而,在答案中提出的解决scheme仍然很好

Solutions Collecting From Web of "VectorDrawable与GoogleMap BitmapDescriptor"

可能的解决方法:

 private BitmapDescriptor getBitmapDescriptor(int id) { Drawable vectorDrawable = context.getDrawable(id); int h = ((int) Utils.convertDpToPixel(42, context)); int w = ((int) Utils.convertDpToPixel(25, context)); vectorDrawable.setBounds(0, 0, w, h); Bitmap bm = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(bm); vectorDrawable.draw(canvas); return BitmapDescriptorFactory.fromBitmap(bm); } 

这里是另一个参考: http : //qiita.com/konifar/items/aaff934edbf44e39b04a

 public class ResourceUtil { @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; } private static Bitmap getBitmap(VectorDrawableCompat 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; } public static Bitmap getBitmap(Context context, @DrawableRes int drawableResId) { Drawable drawable = ContextCompat.getDrawable(context, drawableResId); if (drawable instanceof BitmapDrawable) { return ((BitmapDrawable) drawable).getBitmap(); } else if (drawable instanceof VectorDrawableCompat) { return getBitmap((VectorDrawableCompat) drawable); } else if (drawable instanceof VectorDrawable) { return getBitmap((VectorDrawable) drawable); } else { throw new IllegalArgumentException("Unsupported drawable type"); } } } 

根据bug报告 ( 由vaughandroid发布 – 谢谢!)暂时不支持使用VectorDrawable。 请参阅错误报告中的评论以获取更多信息。

以下是Google地图小组build议的解决方法:

 /** * Demonstrates converting a {@link Drawable} to a {@link BitmapDescriptor}, * for use as a marker icon. */ private BitmapDescriptor vectorToBitmap(@DrawableRes int id, @ColorInt int color) { Drawable vectorDrawable = ResourcesCompat.getDrawable(getResources(), id, null); 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()); DrawableCompat.setTint(vectorDrawable, color); vectorDrawable.draw(canvas); return BitmapDescriptorFactory.fromBitmap(bitmap); } 

用这种方式:

 // Vector drawable resource as a marker icon. mMap.addMarker(new MarkerOptions() .position(ALICE_SPRINGS) .icon(vectorToBitmap(R.drawable.ic_android, Color.parseColor("#A4C639"))) .title("Alice Springs")); 

vector的设色是一种奖励

VectorDrawableBitmapDescriptor没有色调

 private BitmapDescriptor getBitmapDescriptor(@DrawableRes int id) { Drawable vectorDrawable = ResourcesCompat.getDrawable(getResources(), id, null); 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 BitmapDescriptorFactory.fromBitmap(bitmap); } 

谢谢@lbarbosa