Android 中的 Google 地图聚类

2023-12-19

我想在 android 中更改集群图标,如下所示。在一个圆圈中将有一个图像视图和一个文本视图。

我的自定义图标代码

  private class ItemRenderer extends DefaultClusterRenderer<ClusterPopupList> {
    private final IconGenerator mIconGenerator = new IconGenerator(getApplicationContext());
    private final IconGenerator mClusterIconGenerator = new IconGenerator(getApplicationContext());
    private final int mDimension;

    public ItemRenderer() {
        super(getApplicationContext(), map, mClusterManager);

        View multiProfile = getLayoutInflater().inflate(R.layout.multi_profile,null);
        mClusterIconGenerator.setContentView(multiProfile);
        mImageView = new ImageView(getApplicationContext());
        mDimension = (int) getResources().getDimension(R.dimen.custom_profile_image);
        mImageView.setLayoutParams(new ViewGroup.LayoutParams(mDimension, mDimension));
        mIconGenerator.setContentView(mImageView);
    }

    @Override
    protected void onBeforeClusterItemRendered(ClusterPopupList item, MarkerOptions markerOptions) {
        mImageView.setImageResource(item.profilePhoto);
        Bitmap icon = mIconGenerator.makeIcon();
        markerOptions.icon(BitmapDescriptorFactory.fromBitmap(icon));
        super.onBeforeClusterItemRendered(item, markerOptions);

    }

    @Override
    protected void onBeforeClusterRendered(Cluster<ClusterPopupList> cluster, MarkerOptions markerOptions) {
        List<Drawable> profilePhotos = new ArrayList<Drawable>(Math.min(4, cluster.getSize()));
        int width = mDimension;
        int height = mDimension;

        for (ClusterPopupList p : cluster.getItems()) {
            // Draw 4 at most.
            if (profilePhotos.size() == 4) break;
            Drawable drawable = getResources().getDrawable(p.profilePhoto);
            drawable.setBounds(0, 0, width, height);
            profilePhotos.add(drawable);
        }

        Bitmap icon = mClusterIconGenerator.makeIcon(String.valueOf(cluster.getSize()));
        markerOptions.icon(BitmapDescriptorFactory.fromBitmap(icon));
    }

    @Override
    protected void onClusterRendered(Cluster<ClusterPopupList> cluster, Marker marker) {
        super.onClusterRendered(cluster, marker);
    }

    @Override
    public ClusterPopupList getClusterItem(Marker marker) {

        return super.getClusterItem(marker);
    }

    @Override
    protected boolean shouldRenderAsCluster(Cluster<ClusterPopupList> cluster) {
        return cluster.getSize() > 1;
    }
}

多配置文件.xml

    <?xml version="1.0" encoding="utf-8"?>
   <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<de.hdodenhof.circleimageview.CircleImageView
    android:id="@+id/image"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:background="@android:color/transparent"
    android:src="@drawable/icon_cluster_count"/>

<TextView
    android:id="@id/text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="@style/Bubble.TextAppearance.Light"
    android:paddingLeft="@dimen/custom_profile_padding"
    android:paddingRight="@dimen/custom_profile_padding"
    android:layout_below="@id/image"
    android:text="150"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="8dip"
    android:textColor="@color/theme_color"
    android:alpha=".8"/>
   </RelativeLayout>

Output :

我想实现像圆圈中的第一个图像一样的簇图标。我在相对布局中使用了圆形背景,但这不起作用。


用它来删除背景

mClusterIconGenerator.setBackground(null)

将背景设置为给定的 Drawable,或删除背景。 @param background 用作背景的 Drawable,或 null 来删除背景。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Android 中的 Google 地图聚类 的相关文章

随机推荐