Strange drawing artefact using custom drawable with MapView

风流意气都作罢 提交于 2020-01-15 05:42:29

问题


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:


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


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?


回答1:


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.



来源:https://stackoverflow.com/questions/3825358/strange-drawing-artefact-using-custom-drawable-with-mapview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!