安卓百度地图画线问题出现断裂

这篇博客介绍了在安卓系统中,由于图片被读取后尺寸改变,不符合百度地图对纹理图片2的整数幂宽高的要求,从而导致画线时出现问题。为解决此问题,作者提供了代码示例,展示如何通过`resizeImage`方法调整图片尺寸,确保其满足2的倍数要求,并成功在百度地图上绘制折线。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  • 出现的原因

百度地图要求纹理图片宽高须是2的整数幂,如16*64。百度地图画线链接

我们自己系统的图片虽然是大小是2的倍数,但是安卓读取图片后把大小给改变了,导致长宽不是2的倍数,因此需要重新定义图片大小。

  • 代码如下


    private void showXunchaLine(List<LatLng> points) {

        try {

            Bitmap bitmap = BitmapFactory.decodeResource(mActivity.getResources(), R.drawable.wenli_jiantou2);
            Bitmap bitmap_ = resizeImage(bitmap, 16, 64);
            BitmapDescriptor mRedTexture = BitmapDescriptorFactory.fromBitmap(bitmap_);

            List<BitmapDescriptor> textureList = new ArrayList<BitmapDescriptor>();
            textureList.add(mRedTexture);

            //添加纹理索引
            List<Integer> indexList = new ArrayList<>();
            for (int i = 0; i < textureList.size(); i++) {
                indexList.add(i);
            }

            //设置折线的属性
            OverlayOptions mOverlayOptions = new PolylineOptions()
                    .width(15)
                    .color(Color.parseColor("#5c96fa"))
                    .points(points)
                    .dottedLine(true)
                    .customTextureList(textureList)
                    .textureIndex(indexList);
            //在地图上绘制折线
            //mPloyline 折线对象
            mBaiduMap.addOverlay(mOverlayOptions);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static Bitmap resizeImage(Bitmap bm, int newWidth, int newHeight) {
        Bitmap newbm = null;
        if (bm != null) {
            // 获得图片的宽高
            int width = bm.getWidth();
            int height = bm.getHeight();
            // 计算缩放比例
            float scaleWidth = ((float) newWidth) / width;
            float scaleHeight = ((float) newHeight) / height;
            // 取得想要缩放的matrix参数
            Matrix matrix = new Matrix();
            matrix.postScale(scaleWidth, scaleHeight);
            // 得到新的图片
            newbm = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, true);
        }
        return newbm;
    }

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值