content: package com.amap.apis.cluster;
  
  import android.app.Activity;
  import android.content.Context;
  import android.graphics.Bitmap;
  import android.graphics.Canvas;
  import android.graphics.Color;
  import android.graphics.Paint;
  import android.graphics.RectF;
  import android.graphics.drawable.BitmapDrawable;
  import android.graphics.drawable.Drawable;
  import android.os.Bundle;
  
  import com.amap.api.maps.AMap;
  import com.amap.api.maps.CameraUpdateFactory;
  import com.amap.api.maps.MapView;
  import com.amap.api.maps.model.LatLng;
  import com.amap.api.maps.model.LatLngBounds;
  import com.amap.api.maps.model.Marker;
  import com.amap.apis.cluster.demo.RegionItem;
  
  import java.util.ArrayList;
  import java.util.HashMap;
  import java.util.List;
  import java.util.Map;
  
  public class MainActivity extends Activity implements ClusterRender,
          AMap.OnMapLoadedListener, ClusterClickListener {
  
  
      private MapView mMapView;
      private AMap mAMap;
  
      private int clusterRadius = 100;
  
      private Map<Integer, Drawable> mBackDrawAbles = new HashMap<Integer, Drawable>();
  
      private ClusterOverlay mClusterOverlay;
  
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
          mMapView = (MapView) findViewById(R.id.map);
          mMapView.onCreate(savedInstanceState);
          init();
  
      }
  
      private void init() {
          if (mAMap == null) {
              // 初始化地图
              mAMap = mMapView.getMap();
              mAMap.setOnMapLoadedListener(this);
              //点击可以动态添加点
              mAMap.setOnMapClickListener(new AMap.OnMapClickListener() {
                  @Override
                  public void onMapClick(LatLng latLng) {
                      double lat = Math.random() + 39.474923;
                      double lon = Math.random() + 116.027116;
  
                      LatLng latLng1 = new LatLng(lat, lon, false);
                      RegionItem regionItem = new RegionItem(latLng1,
                              "test");
                      mClusterOverlay.addClusterItem(regionItem);
  
                  }
              });
          }
      }
  
  
      protected void onResume() {
          super.onResume();
          mMapView.onResume();
      }
  
      protected void onPause() {
          super.onPause();
          mMapView.onPause();
      }
  
      protected void onDestroy() {
          super.onDestroy();
          //销毁资源
          mClusterOverlay.onDestroy();
          mMapView.onDestroy();
      }
  
      @Override
      public void onMapLoaded() {
          //添加测试数据
          new Thread() {
              public void run() {
  
                  List<ClusterItem> items = new ArrayList<ClusterItem>();
  
                  //随机10000个点
                  for (int i = 0; i < 10000; i++) {
  
                      double lat = Math.random() + 39.474923;
                      double lon = Math.random() + 116.027116;
  
                      LatLng latLng = new LatLng(lat, lon, false);
                      RegionItem regionItem = new RegionItem(latLng,
                              "test" + i);
                      items.add(regionItem);
  
                  }
                  mClusterOverlay = new ClusterOverlay(mAMap, items,
                          dp2px(getApplicationContext(), clusterRadius),
                          getApplicationContext());
                  mClusterOverlay.setClusterRenderer(MainActivity.this);
                  mClusterOverlay.setOnClusterClickListener(MainActivity.this);
  
              }
  
          }
  
                  .start();
  
  
      }
  
  
  
      @Override
      public void onClick(Marker marker, List<ClusterItem> clusterItems) {
  
          LatLngBounds.Builder builder = new LatLngBounds.Builder();
          for (ClusterItem clusterItem : clusterItems) {
              builder.include(clusterItem.getPosition());
          }
          LatLngBounds latLngBounds = builder.build();
          mAMap.animateCamera(CameraUpdateFactory.newLatLngBounds(latLngBounds, 0)
          );
      }
  
      @Override
      public Drawable getDrawAble(int clusterNum) {
          int radius = dp2px(getApplicationContext(), 80);
          if (clusterNum == 1) {
              Drawable bitmapDrawable = mBackDrawAbles.get(1);
              if (bitmapDrawable == null) {
                  bitmapDrawable =
                          getApplication().getResources().getDrawable(
                                  R.drawable.icon_openmap_mark);
                  mBackDrawAbles.put(1, bitmapDrawable);
              }
  
              return bitmapDrawable;
          } else if (clusterNum < 5) {
  
              Drawable bitmapDrawable = mBackDrawAbles.get(2);
              if (bitmapDrawable == null) {
                  bitmapDrawable = new BitmapDrawable(null, drawCircle(radius,
                          Color.argb(159, 210, 154, 6)));
                  mBackDrawAbles.put(2, bitmapDrawable);
              }
  
              return bitmapDrawable;
          } else if (clusterNum < 10) {
              Drawable bitmapDrawable = mBackDrawAbles.get(3);
              if (bitmapDrawable == null) {
                  bitmapDrawable = new BitmapDrawable(null, drawCircle(radius,
                          Color.argb(199, 217, 114, 0)));
                  mBackDrawAbles.put(3, bitmapDrawable);
              }
  
              return bitmapDrawable;
          } else {
              Drawable bitmapDrawable = mBackDrawAbles.get(4);
              if (bitmapDrawable == null) {
                  bitmapDrawable = new BitmapDrawable(null, drawCircle(radius,
                          Color.argb(235, 215, 66, 2)));
                  mBackDrawAbles.put(4, bitmapDrawable);
              }
  
              return bitmapDrawable;
          }
      }
  
      private Bitmap drawCircle(int radius, int color) {
  
          Bitmap bitmap = Bitmap.createBitmap(radius * 2, radius * 2,
                  Bitmap.Config.ARGB_8888);
          Canvas canvas = new Canvas(bitmap);
          Paint paint = new Paint();
          RectF rectF = new RectF(0, 0, radius * 2, radius * 2);
          paint.setColor(color);
          canvas.drawArc(rectF, 0, 360, true, paint);
          return bitmap;
      }
  
      /**
       * 根据手机的分辨率从 dp 的单位 转成为 px(像素)
       */
      public int dp2px(Context context, float dpValue) {
          final float scale = context.getResources().getDisplayMetrics().density;
          return (int) (dpValue * scale + 0.5f);
      }
  
  
  }
  