• android 百度地图 通过剪裁图片添加 Marker


    初始化百度地图:

    1 private void initViews() {
    2 4         mMapView = (MapView) findViewById(R.id.bmapView);
    5         mBaiduMap = mMapView.getMap();
    6         // 初始化地图范围级别
    7         MapStatusUpdate msu = MapStatusUpdateFactory.zoomTo(14.0f);
    8         mBaiduMap.setMapStatus(msu);
    9     }

    --------------------华丽分割线-----------------------

    // 初始化bitmap 信息,不用时及时 recycle
    BitmapDescriptor friend_1;

    初始化剪裁的图片(此处为生成圆形图片):

    1 friend_1 = BitmapDescriptorFactory.fromBitmap(
    2                     BMapUtil.createCircleImage(BitmapFactory.decodeResource(getResources(), R.drawable.friend_1), 90));

    圆形图片生成方法:

     1 /**
     2      * 根据原图和变长绘制圆形图片
     3      * @param source
     4      * @param min
     5      * @return
     6      */
     7     public static Bitmap createCircleImage(Bitmap source, int radius) {
     8         Bitmap scaledSrcBmp;
     9         int diameter = radius * 2;
    10         // 为了防止宽高不相等,造成圆形图片变形,因此截取长方形中处于中间位置最大的正方形图片
    11         int bmpWidth = source.getWidth();
    12         int bmpHeight = source.getHeight();
    13         int squareWidth = 0, squareHeight = 0;
    14         int x = 0, y = 0;
    15         Bitmap squareBitmap;
    16         if (bmpHeight > bmpWidth) {// 高大于宽
    17             squareWidth = squareHeight = bmpWidth;
    18             x = 0;
    19             y = (bmpHeight - bmpWidth) / 2;
    20             // 截取正方形图片
    21             squareBitmap = Bitmap.createBitmap(source, x, y, squareWidth,
    22                     squareHeight);
    23         } else if (bmpHeight < bmpWidth) {// 宽大于高
    24             squareWidth = squareHeight = bmpHeight;
    25             x = (bmpWidth - bmpHeight) / 2;
    26             y = 0;
    27             squareBitmap = Bitmap.createBitmap(source, x, y, squareWidth,
    28                     squareHeight);
    29         } else {
    30             squareBitmap = source;
    31         }
    32         if (squareBitmap.getWidth() != diameter
    33                 || squareBitmap.getHeight() != diameter) {
    34             scaledSrcBmp = Bitmap.createScaledBitmap(squareBitmap, diameter,
    35                     diameter, true);
    36         } else {
    37             scaledSrcBmp = squareBitmap;
    38         }
    39         Bitmap output = Bitmap.createBitmap(scaledSrcBmp.getWidth(),
    40                 scaledSrcBmp.getHeight(), Config.ARGB_8888);
    41         Canvas canvas = new Canvas(output);
    42 
    43         Paint paint = new Paint();
    44         Rect rect = new Rect(0, 0, scaledSrcBmp.getWidth(),
    45                 scaledSrcBmp.getHeight());
    46 
    47         paint.setAntiAlias(true);
    48         paint.setFilterBitmap(true);
    49         paint.setDither(true);
    50         canvas.drawARGB(0, 0, 0, 0);
    51         canvas.drawCircle(scaledSrcBmp.getWidth() / 2,
    52                 scaledSrcBmp.getHeight() / 2, scaledSrcBmp.getWidth() / 2,
    53                 paint);
    54         paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
    55         canvas.drawBitmap(scaledSrcBmp, rect, rect, paint);
    56         bmp = null;
    57         squareBitmap = null;
    58         scaledSrcBmp = null;
    59         return output;
    60     }

    初始化位置信息:

    1 LatLng position_1 = new LatLng(39.963175, 116.400244);

    初始化OverlayOptions:

    1 OverlayOptions ooA = new MarkerOptions().position(position_1).icon(friend_1)
    2                 .zIndex(9).draggable(true);

    再通过addOverlay添加到地图:

    1 mMarkerA = (Marker) (mBaiduMap.addOverlay(ooA));

    注册setOnMarkerClickListener:

    mBaiduMap.setOnMarkerClickListener(new OnMarkerClickListener() {
                public boolean onMarkerClick(final Marker marker) {
           // todo 判断mMarkerA 的点击事件    
       }
    }

    当退出时,释放资源:

    @Override
        protected void onPause() {
            // MapView的生命周期与Activity同步,当activity挂起时需调用MapView.onPause()
            mMapView.onPause();
            super.onPause();
        }
    
        @Override
        protected void onResume() {
            // MapView的生命周期与Activity同步,当activity恢复时需调用MapView.onResume()
            mMapView.onResume();
            super.onResume();
        }
    
        @Override
        protected void onDestroy() {
            // MapView的生命周期与Activity同步,当activity销毁时需调用MapView.destroy()
            mMapView.onDestroy();
            super.onDestroy();
            // 回收 bitmap 资源
            friend_1.recycle();
        }
  • 相关阅读:
    es6 简介
    npm 快速开发技巧
    css清除浮动方法
    mui 总结
    7种 JS 创建对象的经典方式
    JavaScript 的 this 原理
    使用定时器
    dom 操作及方法
    JavaScript的6种继承方式及其优缺点
    贪吃蛇游戏代码
  • 原文地址:https://www.cnblogs.com/CharlesGrant/p/4813968.html
Copyright © 2020-2023  润新知