如何使用图像标记创建自定义可绘制图钉

How to create a custom drawable pin with image marker

我正在尝试创建与以下内容相同的自定义标记图像:

我创建了一个带有背景和内部 CircleImageView 的布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/user_marker_layout"
    android:layout_width="50dp"
    android:background="@drawable/ic_map_person_circle"
    android:layout_height="50dp">

        <de.hdodenhof.circleimageview.CircleImageView
            android:id="@+id/user_marker_icon"
            android:layout_centerHorizontal="true"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:layout_marginTop="3dp"
            app:civ_border_color="#fff"
            android:layout_alignParentTop="true"

            android:contentDescription="@null"
            android:src="@drawable/ic_pin_user" />
             />
</RelativeLayout>

其中 ic_map_person_circle 可绘制对象是:

并通过代码:

Glide.with(mContext).load(imageUri)
.placeholder(R.drawable.ic_default_user_image)
.error(R.drawable.ic_default_user_image)
.priority(Priority.HIGH)
.into(new SimpleTarget<GlideDrawable>() {
    @Override
    public void onResourceReady(GlideDrawable resource, GlideAnimation<? super GlideDrawable> glideAnimation) {
        marker.setIcon(BitmapDescriptorFactory.fromBitmap(BitmapUtil.getMarkerBitmapFromView(resource,mContext)));
    }
});



public static Bitmap getMarkerBitmapFromView(Drawable drawable, Context context) {
    View customMarkerView =
            ((LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.circle_image_on_map, null);


    CircleImageView markerImage = (CircleImageView) customMarkerView.findViewById(R.id.user_marker_icon);
    customMarkerView.setBackground(context.getResources().getDrawable(R.drawable.ic_map_person_circle));
    markerImage.setImageDrawable(drawable);

    customMarkerView.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
    customMarkerView.layout(0, 0, customMarkerView.getMeasuredWidth(), customMarkerView.getMeasuredHeight());
    customMarkerView.buildDrawingCache();

    Bitmap returnedBitmap = Bitmap.createBitmap(customMarkerView.getMeasuredWidth(), customMarkerView.getMeasuredHeight(),
            Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(returnedBitmap);


    customMarkerView.draw(canvas);
    return returnedBitmap;

}

但我仍然得到相同的结果,只有圆形图像:

可能是什么问题?

我解决了将背景图像从外部布局移动到 CircleImageView 的问题:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/user_marker_layout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

        <de.hdodenhof.circleimageview.CircleImageView
            android:id="@+id/user_marker_icon"
            android:layout_centerHorizontal="true"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:background="@drawable/ic_map_person_circle"
            android:paddingLeft="12dp"
            android:paddingRight="12dp"
            android:paddingBottom="12dp"
            app:civ_border_color="#fff"
            android:layout_alignParentTop="true"

            android:contentDescription="@null"
            android:src="@drawable/ic_pin_user" />
             />
</RelativeLayout>