Android 画布绘制路径已关闭

Android 画布绘制路径已关闭,android,android-canvas,Android,Android Canvas,我一直在尝试在谷歌地图中填充我正在绘制的多边形,但是 我弄不好。在下面的代码中,当我只使用drawPaint时,它会在正确的位置绘制,但当我使用drawPath时,它会在不同的位置绘制位图 Bitmap fillBMP = PatternGenerator.makePattern(PatternGenerator.PATTERN_STYLE_X, lineColor); BitmapShader fillBMPshader = new BitmapShader(fillBMP, BitmapSh

我一直在尝试在谷歌地图中填充我正在绘制的多边形,但是 我弄不好。在下面的代码中,当我只使用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();
-23.725012137.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));
moveTo(pos0.x,pos0.y);
对于(int i=1;i
我使用moveTo然后lineTo并关闭路径

// 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);

这里缺少什么吗?

我已经通过获取路径对象的边界矩形修复了这个问题。 并使用RectF的坐标绘制位图 并使用RectF的.left和.top偏移起始位置 这条路的尽头

// 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);