gpt4 book ai didi

android - 在 osmdroid 奖励包中在多边形上绘制网格

转载 作者:行者123 更新时间:2023-11-30 01:37:49 27 4
gpt4 key购买 nike

我搜索了所有互联网但没有找到问题的答案。我正在使用 osmdroid 并且我想在多边形上添加网格,如图所示。我找到了一个类似的 question在 stackoverflow 但这个问题没有答案。那么请告诉我这可能吗?

enter image description here

最佳答案

@Mker 给出了一个很好的开始点:BitmapShader .

这是一个示例代码:

public class GridPolygon extends Polygon {

private BitmapShader bitmapShader;

public GridPolygon(Context ctx) {
super(ctx);
}

public void setPatternBMP(@NonNull final Bitmap patternBMP) {
bitmapShader = new BitmapShader(patternBMP, Shader.TileMode.REPEAT, Shader.TileMode.REPEAT);
mFillPaint.setShader(bitmapShader);
}
}

用法:

final GridPolygon polygon = new GridPolygon(context);
polygon.setPoints(geoData);
polygon.setFillColor(fillColor);
polygon.setStrokeColor(strokeColor);
polygon.setStrokeWidth(strokeWidth);
polygon.setPatternBMP(BitmapFactory.decodeResource(getResources(), R.drawable.pattern));
map.getOverlays().add(polygon);
map.invalidate();

但是如果您尝试移动多边形,您可能会感到困惑 - 位图不想移动:

description

为避免这种情况,您应该计算着色器的偏移量:

public class GridPolygon extends Polygon {

private BitmapShader bitmapShader;
private IGeoPoint lastCenterGeoPoint;
private int xOffset = 0;
private int yOffset = 0;

public GridPolygon(Context ctx) {
super(ctx);
}

public void setPatternBMP(@NonNull final Bitmap patternBMP) {
bitmapShader = new BitmapShader(patternBMP, Shader.TileMode.REPEAT, Shader.TileMode.REPEAT);
mFillPaint.setShader(bitmapShader);
}

protected void recalculateMatrix(@NonNull final MapView mapView) {
//final int mapSize = TileSystem.MapSize(mapView.getZoomLevel());

final Projection projection = mapView.getProjection();
final IGeoPoint geoPoint = mapView.getMapCenter();
if (lastCenterGeoPoint == null) lastCenterGeoPoint = geoPoint;

final Point point = projection.toPixels(geoPoint, null);
final Point lastCenterPoint = projection.toPixels(lastCenterGeoPoint, null);

xOffset += lastCenterPoint.x - point.x;
yOffset += lastCenterPoint.y - point.y;

xOffset %= 100; // 100 is pixel size of shader image
yOffset %= 100;

final Matrix matrix = new Matrix();
matrix.reset();
matrix.setScale(1,1);
matrix.preTranslate(xOffset, yOffset);
//matrix.setTranslate(xOffset, yOffset);
bitmapShader.setLocalMatrix(matrix);

mFillPaint.setShader(bitmapShader);

lastCenterGeoPoint = geoPoint;
}

@Override
protected void draw(Canvas canvas, MapView mapView, boolean shadow) {
recalculateMatrix(mapView);
super.draw(canvas, mapView, shadow);
}
}

结果:

enter image description here

Full source code .

关于android - 在 osmdroid 奖励包中在多边形上绘制网格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34907526/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com