关于BaiduMap自定义覆盖物

最近公司要求做一个类似“支付宝到位”功能,要求头像从网上获取,还要求有背景,点击有背景 会进行更换。
BaiduMap提供我们的覆盖物的API中,只可以替换icon或icons

MarkerOptions ooA = new MarkerOptions().position(new LatLng(new Double(info.getBo_lat()), new Double(info.getBo_lng())))
                    .icon(bitmapDescriptor).extraInfo(bundle)
                    .zIndex(9).draggable(true);

可以看出只是提供给我们替换bitmap的方法,没有提供一个addView或者replaceView的方法。

1、思路

用一个ImageView,设置背景,用Glide去加载网络图片,然后得到从ImageView得到bitmap,设置给mapView

2、踩到的坑

  • 使用infoWindow的形式,但是infoWindow只能显示一个,查了很多资料,在android这方面没有找找解决的方式
  • 使用canvas绘制的方式,先绘制背景bitmap,在绘制头像,但是在将bitmap剪裁成圆形的bitmap时出了问题,setXfermode不管使用哪种规则,都会有造成bitmap上显示不完整。
  • 既然bitmap剪裁成圆形的bitmap除了问题,我自己写了一个CircleImageView,核心方法如下

    float h = getMeasuredHeight()- 3.0f;
    float w = getMeasuredWidth()- 3.0f;
    if (path == null) {
    path = new Path();
    path.addCircle(
    w/2.0f
    , h/2.0f
    , (float) Math.min(w/2.0f, (h / 2.0))
    , Path.Direction.CCW);
    path.close();
    }
    cns.drawCircle(w/2.0f, h/2.0f, Math.min(w/2.0f, h / 2.0f) + 1.5f, paint);
    int saveCount = cns.getSaveCount();
    cns.save();
    cns.setDrawFilter(mPaintFlagsDrawFilter);
    cns.clipPath(path, Region.Op.REPLACE);
    cns.setDrawFilter(mPaintFlagsDrawFilter);
    cns.drawColor(Color.WHITE);
    super.onDraw(cns);
    cns.restoreToCount(saveCount);

    采用剪裁画布的方式得到圆形,可以取得圆形bitmap了,但是此时因为我数学不好,总是不能把他画到正确的位置上,只能暂时搁浅

  • 踩到了Glide加载网络图片显示慢的坑,bitmap经常性的不能得到正确的。
  • 使用

    imageView.setDrawingCacheEnabled(true);
    Bitmap bitmap = imageView.getDrawingCache();

    方式得到bitmap,但是总是为空,卡了很久,点进源码一看,有个判定

        ` if ((mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING) {
                return null;
           }`
    

    焕然大悟,未绘制的view只能得到空的bitmap,我的这个view不就没有显示吗!找到问题就好解决了,先想法让view显示,再取得bitmap,不就可以了

  • 最后一个坑,竟然是因为我自己写的CircleImageView,因为对画布进行了剪裁,无法正确的得到背景图片。

3、实现方式

//先用Glide加载图片,防止无法取得正确bitmap
    private void initBitmap(List<OrderList.DatasBean.OrderListBean> orderList) {

        for (OrderList.DatasBean.OrderListBean orderListBean : orderList) {
            View view = View.inflate(getActivity(), R.layout.map_icon, null);
            ImageView mapIcon = (ImageView) view.findViewById(R.id.iv_icon);
            Glide.with(getActivity())
                    .load(orderListBean.getMember_avatar())
                    .into(mapIcon);

            mDescriptors.add(view);
        }

    }
            View view = mDescriptors.get(i);
            InfoWindow infoWindow = new InfoWindow(view, new LatLng(new Double(info.getBo_lat()), new Double(info.getBo_lng())), 0);
            //显示InfoWindow
            mBaiduMap.showInfoWindow(infoWindow);
            //取得正确的bitmap
            ImageView imageView = (ImageView) view.findViewById(R.id.iv_icon);
            imageView.setDrawingCacheEnabled(true);
            Bitmap bitmap = imageView.getDrawingCache();
            BitmapDescriptor bitmapDescriptor = BitmapDescriptorFactory.fromBitmap(bitmap);

            MarkerOptions ooA = new MarkerOptions().position(new LatLng(new Double(info.getBo_lat()), new Double(info.getBo_lng())))
                    .icon(bitmapDescriptor).extraInfo(bundle)
                    .zIndex(9).draggable(true);
            // 掉下动画
            ooA.animateType(MarkerOptions.MarkerAnimateType.drop);
            mBaiduMap.addOverlay(ooA);
            //关闭InfoWindow
            if (i == mAllOrderList.size() - 1) {
                mBaiduMap.hideInfoWindow();
            }

ok,完美显示

4、感悟

遇到问题总是想着百度,不自己沉入代码中研究,总是与在百度中有无法解决的问题,有时候深入代码中学习,才能得到很好的解决方式。

附效果如图,第一次上传,不会压缩

这里写图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值