Android百度地图点聚合聚合点的样式修改-自定义聚合点样式

Android默认的聚合点样式为蓝底白字的圆圈,我需要修改其样式   

1.修改聚合点图标

找到text_bubble.xml文件.在

<com.baidu.mapapi.clusterutil.ui.RotationLayout
里面设置你需要显示的样式

我的是

android:background="@drawable/red_66x65"
<?xml version="1.0" encoding="utf-8"?>
<!--
  ~ Copyright (C) 2015 Baidu, Inc. All Rights Reserved.
  -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <com.baidu.mapapi.clusterutil.ui.RotationLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/red_66x65"
        android:orientation="vertical">

        <TextView
            android:id="@+id/text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:paddingBottom="5dp"
            android:paddingLeft="10dp"
            android:paddingRight="10dp"
            android:paddingTop="5dp"/>

    </com.baidu.mapapi.clusterutil.ui.RotationLayout>

</LinearLayout>

修改后预览效果如下

然后运行项目你会发现  虽然聚合点样式改了 但是怎么还有默认的背景蓝色  而不是自己想要的透明底色的图标

2.去掉默认的蓝色

找到

DefaultClusterRenderer类查看源码




发现在构造函数里面有一句
 mIconGenerator.setBackground(makeClusterBackground());

进入makeCluesterBackground()方法

    private LayerDrawable makeClusterBackground() {
        mColoredCircleBackground = new ShapeDrawable(new OvalShape());
        ShapeDrawable outline = new ShapeDrawable(new OvalShape());
        outline.getPaint().setColor(0x80ffffff); // Transparent white.
        LayerDrawable background = new LayerDrawable(new Drawable[]{outline, mColoredCircleBackground});
        int strokeWidth = (int) (mDensity * 3);
        background.setLayerInset(1, strokeWidth, strokeWidth, strokeWidth, strokeWidth);
        return background;
    }

发现颜色是通过mColoredCircleBackground对象得到的  然后搜索mColoredCircleBackground

找到其设置颜色的位置在onBeforeClusterRendered()方法里面

如图将

mColoredCircleBackground.getPaint().setColor(getColor(bucket));

修改为

mColoredCircleBackground.getPaint().setColor(Color.TRANSPARENT);

这样就去掉了默认的圆形背景颜色

背景颜色修改完  发现红色背景跟白色的字好像不搭  怎么办  要修改聚合点的字体颜色了

然后回头去text_bubble.xml文件中修改Textview的textcolor?这是没效果的

仔细查看发现

其实还是在

DefaultClusterRenderer类里面,你会看到构造方法里面有一个
mIconGenerator.setTextAppearance(R.style.ClusterIcon_TextAppearance);

然后点进style文件将textcolor改为自己需要的样式就好了

至于修改点聚合每一个item的的样式,百度地图demo里面有   getBitmapDescriptor()的resource替换成自己需要的图标就行了

  /**
     * 每个Marker点,包含Marker点坐标以及图标
     */
    public class MyItem implements ClusterItem {
        private final LatLng mPosition;
        
        public MyItem(LatLng latLng) {
            mPosition = latLng;
        }

        @Override
        public LatLng getPosition() {
            return mPosition;
        }

        //设置marker图标
        @Override
        public BitmapDescriptor getBitmapDescriptor() {
            return BitmapDescriptorFactory
                    .fromResource(R.drawable.icon_gcoding);
        }

        @Override
        public String toString() {
            return "MyItem{" +
                    "mPosition=" + mPosition +
                    '}';
        }
    }

这里参考了https://blog.csdn.net/shaoyezhangliwei/article/details/60959852实现的修改聚合点图标

猜你喜欢

转载自blog.csdn.net/ulddfhv/article/details/81233755