Android 地图开发:解决自定义图标旋转后原始图标依旧存在的问题
要解决图标旋转后原始图标依旧存在的问题,可以在绘制旋转图标前,先清空画布上的内容。可以在每次绘制前调用canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR)方法清空画布,或者使用canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.SRC)方法填充画布为透明色。\n\n修改后的代码如下:\n\njava\nfor (CustomOverlayItem item : mItemList) {\n\tDrawable marker = item.getMarker(0); // 获取标记的图标\n\tfloat rotation = item.getRotation(); // 获取标记的旋转角度\n\tif (marker instanceof BitmapDrawable && rotation != 0) {\n\t BitmapDrawable bitmapDrawable = (BitmapDrawable) marker;\n\t Bitmap bitmap = bitmapDrawable.getBitmap();\n\t Matrix matrix = new Matrix();\n\t matrix.postRotate(rotation);\n\t Bitmap rotatedBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);\n\n\t canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR); // 清空画布\n\n\t IGeoPoint iGeoPoint = item.getPoint();\n\t Point screenPts = mapView.getProjection().toPixels(iGeoPoint, null);\n\t canvas.translate(screenPts.x - (rotatedBitmap.getWidth() / 2), screenPts.y - (rotatedBitmap.getHeight() / 2));\n\t canvas.drawBitmap(rotatedBitmap, 0, -45, null);\n\t}\n}\n\n\n这样在每次绘制旋转图标前,都会清空画布,然后再绘制旋转图标,解决了旋转前的图标依旧存在的问题。
原文地址: https://www.cveoy.top/t/topic/pInF 著作权归作者所有。请勿转载和采集!