BitmapDescriptor的VectorDrawable

BitmapDescriptor的VectorDrawable

本文介绍了使用GoogleMap BitmapDescriptor的VectorDrawable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 VectorDrawable创建 MarkerOptions 的图标时,Google Maps存在问题 BitmapDescriptor / code>,API 5.0 +



用于创建的方法:

  @NonNull 
private BitmapDescriptor getBitmapDescriptor(int id){
返回BitmapDescriptorFactory.fromResource(id);

$ / code>

id 参数包含png drawable,但是如果我用xml中定义的 VectorDrawable 来尝试它,当 googleMap.addMarker(marker) BitmapDescriptor 不为空)。

  11- 05 10:15:05.213 14536-14536 / xxx.xxxxx.app E / AndroidRuntime:FATAL EXCEPTION:main 
过程:xxx.xxxxx.app,PID:14536
java.lang.NullPointerException
在com.google.aaae.a(未知源)
在com.google.maps.api.android.lib6.d.dn。< init>(未知源)
在com。 google.maps.api.android.lib6.d.dm.a(未知来源)
,位于com.google.maps.api.android.lib6.d.ag。< init>(未知来源)
在com.google.maps.api.android.lib6.d.eu.a(未知来源)
在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(未知来源)
来自com.google.android.gms.maps.GoogleMap.addMarker(未知来源)
在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(未知来源)
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)
在android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native方法)
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)

无论如何检索drawable,尝试使用 BitmapFactory.fromResources 和后来的 BitmapDescritpionFactory.fromBitmap 创建位图,但结果是相同的。它只是不能用于矢量绘制。尝试不同的载体,但似乎载体的复杂性不是这里的问题。



有谁知道如何解决这个问题?



@edit

看起来问题不在于 BitmapDescriptior 本身,但而是加载 VectorDrawable ,这是返回不正确的位图。但是,在答案中提出的解决方案仍然很好。

解决方案
可能的解决方法:

  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);
位图bm = Bitmap.createBitmap(w,h,Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bm);
vectorDrawable.draw(canvas);
返回BitmapDescriptorFactory.fromBitmap(bm);
}


I have problem with google maps BitmapDescriptor while creating icon for MarkerOptions using VectorDrawable, API 5.0+

Method used for creation:

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

Everything works great when id argument contains png drawable, however if I try it with VectorDrawable defined in xml, App always crash when googleMap.addMarker(marker) (BitmapDescriptor is not 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.a.a.ae.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)

It doesn't matter how i retrieve drawable, tried creating bitmap using BitmapFactory.fromResources and later BitmapDescritpionFactory.fromBitmap but results are the same. It just won't work with vector drawable. Tried different vectors as well but it's seems that vector complexity is not the issue here.

Does anyone know how to fix this crash?

@edit

It seems like the problem wasn't with the BitmapDescriptior itself, but rather with loading VectorDrawable which was returning incorrect bitmap. However solution proposed in answer is still fine.

解决方案

Possible workaround:

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);
}

这篇关于使用GoogleMap BitmapDescriptor的VectorDrawable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 10:07