Android maps utils cluster icon color

前端 未结 6 1648
谎友^
谎友^ 2020-12-13 07:23

Is there any method to change the background color of the cluster item? (the one that displays the count of the markers, like 100+, 200+ ...). I tried to look into the sourc

6条回答
  •  被撕碎了的回忆
    2020-12-13 07:43

    I took some methods of superclass and partially remade them. Now i have beautiful standard clusters with my own colors.

    public class CustomClusterRenderer extends DefaultClusterRenderer {
    
    private final IconGenerator mIconGenerator;
    private ShapeDrawable mColoredCircleBackground;
    private SparseArray mIcons = new SparseArray();
    private final float mDensity;
    private Context mContext;
    
    public CustomClusterRenderer(Context context, GoogleMap map,
                                 ClusterManager clusterManager) {
        super(context, map, clusterManager);
    
    
        this.mContext = context;
        this.mDensity = context.getResources().getDisplayMetrics().density;
        this.mIconGenerator = new IconGenerator(context);
        this.mIconGenerator.setContentView(this.makeSquareTextView(context));
        this.mIconGenerator.setTextAppearance(
                com.google.maps.android.R.style.ClusterIcon_TextAppearance);
        this.mIconGenerator.setBackground(this.makeClusterBackground());
    }
    
    @Override
    protected void onBeforeClusterRendered(Cluster cluster,
                                           MarkerOptions markerOptions) {
        // Main color
        int clusterColor = mContext.getResources().getColor(R.color.colorPrimary);
    
        int bucket = this.getBucket(cluster);
        BitmapDescriptor descriptor = this.mIcons.get(bucket);
        if(descriptor == null) {
            this.mColoredCircleBackground.getPaint().setColor(clusterColor);
            descriptor = BitmapDescriptorFactory.fromBitmap(
                    this.mIconGenerator.makeIcon(this.getClusterText(bucket)));
            this.mIcons.put(bucket, descriptor);
        }
    
        markerOptions.icon(descriptor);
    }
    
    private SquareTextView makeSquareTextView(Context context) {
        SquareTextView squareTextView = new SquareTextView(context);
        ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(-2, -2);
        squareTextView.setLayoutParams(layoutParams);
        squareTextView.setId(com.google.maps.android.R.id.text);
        int twelveDpi = (int)(12.0F * this.mDensity);
        squareTextView.setPadding(twelveDpi, twelveDpi, twelveDpi, twelveDpi);
        return squareTextView;
    }
    
    private LayerDrawable makeClusterBackground() {
        // Outline color
        int clusterOutlineColor = mContext.getResources().getColor(R.color.colorWhite);
    
        this.mColoredCircleBackground = new ShapeDrawable(new OvalShape());
        ShapeDrawable outline = new ShapeDrawable(new OvalShape());
        outline.getPaint().setColor(clusterOutlineColor);
        LayerDrawable background = new LayerDrawable(
                new Drawable[]{outline, this.mColoredCircleBackground});
        int strokeWidth = (int)(this.mDensity * 3.0F);
        background.setLayerInset(1, strokeWidth, strokeWidth, strokeWidth, strokeWidth);
        return background;
    }
    

    And then set renderer to Cluster Manager

    mClusterManager = new ClusterManager<>(context, mGoogleMap);
    mClusterManager.setRenderer(new CustomClusterRenderer(context, mGoogleMap, mClusterManager));
    

提交回复
热议问题