使用BitmapDescriptor
创建MarkerOptions
图标时,谷歌地图VectorDrawable
存在问题,API 5.0 +
用于创作的方法:
@NonNull
private BitmapDescriptor getBitmapDescriptor(int id) {
return BitmapDescriptorFactory.fromResource(id);
}
当id
参数包含png drawable时,一切都很有效,但是如果我在xml中定义的VectorDrawable
进行尝试,那么当googleMap.addMarker(marker)
(BitmapDescriptor
不为null时,App总会崩溃)。
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)
我如何检索drawable,尝试使用BitmapFactory.fromResources
和更晚BitmapDescritpionFactory.fromBitmap
创建位图并不重要,但结果是相同的。它只是不能使用矢量drawable。尝试了不同的向量,但似乎这里的向量复杂度不是问题。
有谁知道如何修复此崩溃?
@edit
似乎问题不在于BitmapDescriptior
本身,而在于加载VectorDrawable
并返回错误的位图。然而,答案中提出的解决方案仍然没有问题。
答案 0 :(得分:40)
可能的解决方法:
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);
}
答案 1 :(得分:8)
根据the bug report(posted by vaughandroid - 谢谢!)暂时不支持使用VectorDrawable。有关详细信息,请参阅comment in the bug report。
以下是Google地图小组建议的解决方法:
/**
* 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"));
向量的着色是一个奖励
答案 2 :(得分:6)
这是另一个参考: 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");
}
}
}
答案 3 :(得分:3)
VectorDrawable
到BitmapDescriptor
没有色调
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
答案 4 :(得分:3)
或者您可以简单地使用Android KTX
例如:
val markerBitmap = ResourcesCompat.getDrawable(resources, R.drawable.ic_marker, null)?.toBitmap()
val icon = BitmapDescriptorFactory.fromBitmap(markerBitmap)
val marker = MarkerOptions().icon(icon)
参考:.toBitmap()
答案 5 :(得分:2)
科林版
private fun getBitmapDescriptor(context: Context, id: Int): BitmapDescriptor? {
val vectorDrawable: Drawable?
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
vectorDrawable = context.getDrawable(id)
} else {
vectorDrawable = ContextCompat.getDrawable(context, id)
}
if (vectorDrawable != null) {
val w = vectorDrawable.intrinsicWidth
val h = vectorDrawable.intrinsicHeight
vectorDrawable.setBounds(0, 0, w, h)
val bm = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
val canvas = Canvas(bm);
vectorDrawable.draw(canvas);
return BitmapDescriptorFactory.fromBitmap(bm);
}
return null
}
答案 6 :(得分:0)
在Kotlin上也一样
private fun getBitmapDescriptorFromVector(id: Int, context: Context): BitmapDescriptor {
var vectorDrawable: Drawable = context.getDrawable(id)
var h = (24 * getResources().getDisplayMetrics().density).toInt();
var w = (24 * getResources().getDisplayMetrics().density).toInt();
vectorDrawable.setBounds(0, 0, w, h)
var bm = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888)
var canvas = Canvas(bm)
vectorDrawable.draw(canvas)
return BitmapDescriptorFactory.fromBitmap(bm)
}
编辑 @ CoolMind,您完全正确,谢谢-编辑
答案 7 :(得分:0)
我认为这是最简单的解决方案,因为它在MarkerOptions的扩展功能中隐藏了实现:
fun MarkerOptions.icon(context: Context, @DrawableRes vectorDrawable: Int): MarkerOptions {
this.icon(ContextCompat.getDrawable(context, vectorDrawable)?.run {
setBounds(0, 0, intrinsicWidth, intrinsicHeight)
val bitmap = Bitmap.createBitmap(intrinsicWidth, intrinsicHeight, Bitmap.Config.ARGB_8888)
draw(Canvas(bitmap))
BitmapDescriptorFactory.fromBitmap(bitmap)
})
return this
}
因此使用时,最终结果应如下所示:
MarkerOptions().position(myLocation).icon(requireContext(), R.drawable.ic_my_location_map)
如果您在应用中的不同位置有多个地图,则无需复制/粘贴实现到多个类,这将特别有用。