我正在尝试扩展/子类ShapeDrawable。结果真的很令人困惑。我的简单代码应该创建一个小点和一个小三角形。事实上,这些形状确实出现在mapview上,在那里绘制它们。然而,第二个形状略有不同的三角形也出现了,我不知道为什么会这样。为什么我的ShapeDrawable子类会创建一个额外的三角形?
示例SCREENSHOT --->> http://www.activemetrics.com/DrawableProb.png
private class CustomDrawable extends ShapeDrawable
{
public CustomDrawable() //GeoPoint point, MapView mapView)
{
}
public void draw(Canvas canvas)
{
canvas.drawCircle(0, 0, 2/*radius*/, getPaint());
Paint paint = new Paint();
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(2);
paint.setColor(Color.RED);
Path path = new Path();
path.reset();
path.moveTo(0, -10);
path.lineTo(5, 0);
path.lineTo(-5, 0);
path.close();
path.offset(10, 40);
canvas.drawPath(path, paint);
}
}
答案 0 :(得分:0)
我想也许这个问题太难了。也许在这个子类之外的某个地方出现了问题。 (但奇怪的是,幽灵三角形通过删除绘图逻辑而消失了,而且确实发生了。)无论如何,我通过制作一个叠加层来添加到mapview然后重载绘制方法,从而获得了更好的体验。 ...
private class MapOverlay extends com.google.android.maps.Overlay
{
@Override
public boolean draw(Canvas canvas, MapView mapView,
boolean shadow, long when)
{
super.draw(canvas, mapView, shadow);
Bitmap bmp = BitmapFactory.decodeResource(
getResources(), R.drawable.ic_launcher);
//---translate the GeoPoint to screen pixels---
Point screenPts1 = new Point();
mapView.getProjection().toPixels(p1, screenPts1);
//---translate the GeoPoint to screen pixels---
Point screenPts2 = new Point();
mapView.getProjection().toPixels(p2, screenPts2);
//---add the first marker---
canvas.drawBitmap(bmp, screenPts1.x - bmp.getWidth()/2,
screenPts1.y - bmp.getHeight()/2, null);
(...剪断...)