Overridepublic void drawCanvas canvas MapView mapView boolean shadow superdrawcanvas mapView shadow; 绘制每个标记的旋转角度 for CustomOverlayItem item mItemList Drawable marker = itemgetMar
在你的代码中,问题可能出在清除画布上的内容的位置。你当前的方法是在每次循环中都清除画布上的内容,这将导致之前绘制的内容被清除,只留下最后一个旋转后的图标。
为了解决这个问题,你可以在循环外面清除画布上的内容,这样就只需要清除一次,然后在循环内部绘制所有旋转后的图标。以下是修改后的代码:
@Override public void draw(Canvas canvas, MapView mapView, boolean shadow) { super.draw(canvas, mapView, shadow);
// 清除画布上的内容
canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);
// 绘制每个标记的旋转角度
for (CustomOverlayItem item : mItemList) {
Drawable marker = item.getMarker(0); // 获取标记的图标
float rotation = item.getRotation(); // 获取标记的旋转角度
if (marker instanceof BitmapDrawable && rotation != 0) {
BitmapDrawable bitmapDrawable = (BitmapDrawable) marker;
Bitmap bitmap = bitmapDrawable.getBitmap();
// 创建一个旋转后的图标
Matrix matrix = new Matrix();
matrix.postRotate(rotation);
Bitmap rotatedBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
// 绘制旋转后的图标
canvas.save();
IGeoPoint iGeoPoint = item.getPoint();
Point screenPts = mapView.getProjection().toPixels(iGeoPoint, null);
canvas.translate(screenPts.x - (rotatedBitmap.getWidth() / 2), screenPts.y - (rotatedBitmap.getHeight() / 2));
canvas.drawBitmap(rotatedBitmap, 0, -40, null);
canvas.restore();
}
}
}
这样修改后,你应该能够正常地绘制每个旋转后的图标
原文地址: https://www.cveoy.top/t/topic/hJX7 著作权归作者所有。请勿转载和采集!