我得到了上述问题的答案
我从视图中创建了一个位图,然后将其放置在标记上。我的 xml 在下面
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<ImageView
android:id="@+id/img_id"
android:layout_width="@dimen/custom_profile_image"
android:layout_height="@dimen/custom_profile_image"
android:contentDescription="@string/content"
android:scaleType="centerCrop" />
</RelativeLayout>
在 xml 之上膨胀
View viewMarker = ((LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE))
.inflate(R.layout.custom_marker_layout, null);
ImageView myImage = (ImageView) viewMarker.findViewById(R.id.img_id);
在运行时从 url 下载的 imageview 上设置 Imagebitmap
myImage.setImageBitmap(Imagebitmap);
现在将视图对象传递给方法并从中返回位图。像这样
Bitmap bmp=createDrawableFromView(context,viewMarker);
googleMap.addMarker(new MarkerOptions()
.position(new LatLng(latitude, longitude)).title(title)
.snippet(snippet)
.icon(BitmapDescriptorFactory.fromBitmap(bmp)));
现在您可以在任何地方使用返回的位图,那就是您可以使用 xmls 制作自定义标记图标。
public static Bitmap createDrawableFromView(Context context, View view) {
DisplayMetrics displayMetrics = new DisplayMetrics();
((Activity) context).getWindowManager().getDefaultDisplay()
.getMetrics(displayMetrics);
view.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
view.measure(displayMetrics.widthPixels, displayMetrics.heightPixels);
view.layout(0, 0, displayMetrics.widthPixels,
displayMetrics.heightPixels);
view.buildDrawingCache();
Bitmap bitmap = Bitmap.createBitmap(view.getMeasuredWidth(),
view.getMeasuredHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
view.draw(canvas);
return bitmap;
}
它对我有用。谢谢!!!