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

  • 出现的原因

百度地图要求纹理图片宽高须是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;
    }


版权声明:本文为Jermmy1207原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。