使用 MapView 自定义可绘制的奇怪绘图工件

发布于 2024-09-25 14:53:34 字数 2044 浏览 5 评论 0原文

我正在尝试添加自己的可绘制对象并将其用于 MapView 上的一系列叠加层中。可绘制基本上是一个带有黑色轮廓和中间有一个数字的圆形框。

我已经设法使用下面的代码实现了这一点,但是我的框左侧有一个看起来像旗帜的东西,我当然不认为我已经绘制了它 - 所以我想知道它可能是什么。

这是图像的示例: alt text


编辑 - 这是绘制圆形时会发生的情况: alt text


我的代码如下:

自定义可绘制:

public class BikeDrawable extends Drawable {
  int colour;
  String bikes;

public BikeDrawable (int bikes){
    this.bikes = Integer.toString(bikes);
    if (bikes < 4) {
        colour = Color.RED;
    }
    else if (bikes > 3 && bikes < 9){
        colour = Color.argb(244, 255, 127, 42);
    }
    else {
        colour = Color.GREEN;
    }
}
@Override
public void draw(Canvas canvas) {
      Paint rectanglePaint = new Paint();
      rectanglePaint.setColor(colour);
      rectanglePaint.setStyle(Style.FILL);
      RectF rectangle = new RectF(0.0f, 0.0f, 20.0f, 20.0f);
      Paint strokepaint = new Paint();
      strokepaint.setStyle(Paint.Style.STROKE);
      strokepaint.setStrokeWidth(2);
      strokepaint.setARGB(255, 0, 0, 0);

      canvas.drawRoundRect(rectangle, 4.0f, 4.0f, rectanglePaint);
      canvas.drawRoundRect(rectangle, 4.0f, 4.0f, strokepaint);

      Paint textpaint = new Paint();
      textpaint.setARGB(255, 0, 0, 0);
      textpaint.setTextAlign(Paint.Align.CENTER);
      canvas.drawText(bikes, 10, 14, textpaint);
}

@Override
public int getOpacity() {
    return 0;
}

@Override
public void setAlpha(int alpha) {
}

@Override
public void setColorFilter(ColorFilter cf) {
}

}

在 MapView 中使用:

bikeOverlay = new PointsOverlay(start_icon);
BikeDrawable start_1_drawable = new BikeDrawable (start_1.capacity);
OverlayItem start_1_overlayitem = new OverlayItem(new GeoPoint(start_1.lat,start_1.lon), null, null);
start_1_overlayitem.setMarker(start_1_drawable);
mapOverlays.add(bikeOverlay);
bikeOverlay.addOverlay(start_1_overlayitem);

有人知道这里发生了什么吗?它是 OverlayItem 的产物吗?

I'm trying to add my own drawable and use it in a series of overlays on a MapView. The drawable is basically a rounded box with a black outline and a number in the middle.

I have managed to achieve this using the code below, however there is what looks like a flag to the left of my box, which I certainly don't think I have drawn - so I'm wondering what it can be.

This is an example of the image: alt text


Edit - this is what happens if a circle is drawn:
alt text


My code is below:

Custom Drawable:

public class BikeDrawable extends Drawable {
  int colour;
  String bikes;

public BikeDrawable (int bikes){
    this.bikes = Integer.toString(bikes);
    if (bikes < 4) {
        colour = Color.RED;
    }
    else if (bikes > 3 && bikes < 9){
        colour = Color.argb(244, 255, 127, 42);
    }
    else {
        colour = Color.GREEN;
    }
}
@Override
public void draw(Canvas canvas) {
      Paint rectanglePaint = new Paint();
      rectanglePaint.setColor(colour);
      rectanglePaint.setStyle(Style.FILL);
      RectF rectangle = new RectF(0.0f, 0.0f, 20.0f, 20.0f);
      Paint strokepaint = new Paint();
      strokepaint.setStyle(Paint.Style.STROKE);
      strokepaint.setStrokeWidth(2);
      strokepaint.setARGB(255, 0, 0, 0);

      canvas.drawRoundRect(rectangle, 4.0f, 4.0f, rectanglePaint);
      canvas.drawRoundRect(rectangle, 4.0f, 4.0f, strokepaint);

      Paint textpaint = new Paint();
      textpaint.setARGB(255, 0, 0, 0);
      textpaint.setTextAlign(Paint.Align.CENTER);
      canvas.drawText(bikes, 10, 14, textpaint);
}

@Override
public int getOpacity() {
    return 0;
}

@Override
public void setAlpha(int alpha) {
}

@Override
public void setColorFilter(ColorFilter cf) {
}

}

Use in MapView:

bikeOverlay = new PointsOverlay(start_icon);
BikeDrawable start_1_drawable = new BikeDrawable (start_1.capacity);
OverlayItem start_1_overlayitem = new OverlayItem(new GeoPoint(start_1.lat,start_1.lon), null, null);
start_1_overlayitem.setMarker(start_1_drawable);
mapOverlays.add(bikeOverlay);
bikeOverlay.addOverlay(start_1_overlayitem);

Does anyone have any idea what is going on here? Is it an artefact from OverlayItem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

¢好甜 2024-10-02 14:53:34

这是通过重写 PointsOverlay 的 draw() 方法来实现的,如下所示:

@Override
public void draw(Canvas canvas, MapView mapView, boolean shadow)
{
    if(!shadow)
    {
        super.draw(canvas, mapView, false);
    }
}

感谢 etteyafed - 如果您回答了,我会记下您的分数,但我想结束这个。

This was achieved by overriding the draw() method of the PointsOverlay as follows:

@Override
public void draw(Canvas canvas, MapView mapView, boolean shadow)
{
    if(!shadow)
    {
        super.draw(canvas, mapView, false);
    }
}

Thanks to etteyafed - I would have credited you with the points if you had answered, but I wanted to close this off.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文