我一直在尝试填充我在Goole Map中绘制的多边形但是 我做不到。在我下面的代码中,当我只使用drawPaint时,它会在正确的位置绘制,但是当我使用drawPath时,它会在不同的位置绘制位图。
Bitmap fillBMP = PatternGenerator.makePattern(PatternGenerator.PATTERN_STYLE_X, lineColor);
BitmapShader fillBMPshader = new BitmapShader(fillBMP, BitmapShader.TileMode.REPEAT, BitmapShader.TileMode.REPEAT);
Paint paintshader = new Paint();
paintshader.setAntiAlias(true);
paintshader.setShader(fillBMPshader);
android.graphics.Point r1 = projection.toScreenLocation(mLatLngBounds.northeast);
android.graphics.Point r2 = projection.toScreenLocation(mLatLngBounds.southwest);
int width = Math.abs(Math.abs(r1.x) - Math.abs(r2.x));
int height = Math.abs(Math.abs(r2.y) - Math.abs(r1.y)); //Math.abs(r2.y - r1.y);
Bitmap bm = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bm);
Paint p = new Paint();
p.setColor(Color.YELLOW);
//canvas.drawPaint(p); // This works
canvas.drawPath(path, paintshader); // This does not work.
GroundOverlay overlay = map.addGroundOverlay(new GroundOverlayOptions()
.anchor(0f, 0f)
.image(BitmapDescriptorFactory.fromBitmap(bm))
.transparency(0.3f)
.positionFromBounds(mLatLngBounds));
我创建的路径是一系列要点。
-23.725012,137.285156
-24.527135,145.371094
-29.382175,145.898438
-29.993002,138.691406
-23.725012,137.285156
int nPoints = mOuter.mCoordinates.size();
android.graphics.Point pos0 = projection.toScreenLocation(mOuter.mCoordinates.get(0));
path.moveTo(pos0.x, pos0.y);
for ( int i = 1; i < nPoints; i++)
{
builder.include(mOuter.mCoordinates.get(i));
android.graphics.Point pos = projection.toScreenLocation(mOuter.mCoordinates.get(i));
path.lineTo(pos.x, pos.y);
}
path.close();
我使用moveTo然后使用lineTo并关闭路径。
我在这里缺少什么东西吗?
答案 0 :(得分:0)
我已经通过获取Path对象的边界矩来解决了这个问题。 并使用RectF的坐标绘制位图 并使用.left和.top of RectF来偏移起始位置 路径。
// This will get the bounding rect of the path and use
// the width and height of the rect to create the Bitmap.
RectF rectF = new RectF();
path.computeBounds(rectF, true);
int width = (int)rectF.width();
int height = (int)rectF.height();
Bitmap bmPattern = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bmPattern);
// Need to Offset the origin so that it will drawn properly
canvas.translate(rectF.left * -1, rectF.top * -1);
canvas.drawPath(path, paintshader);