Android自定义叠加不绘图

时间:2012-03-08 16:28:26

标签: android overlay android-maps

我正在尝试绘制一个叠加层,它会在该框中显示一个框和文本,但似乎无法显示。

叠加层如下所示

public class StatsOverlay extends Overlay 
{
Paint paint;
double altitude;
float speed;
float distance;
float maxSpeed;
long time;
double calories;
boolean showStats;

public StatsOverlay()
{

}

public void draw(Canvas canvas, MapView mapView, Paint paint, boolean shadow)
{
    super.draw(canvas, mapView, shadow);
    if(showStats == true)
    {
                    paint = new Paint();
        paint.setAntiAlias(true);
        paint.setARGB(80, 245, 245, 245);
        canvas.drawRect(0, 0, 350, 100, paint);
        paint.setTextSize(15);
        paint.setARGB(280, 0, 0, 0);

        canvas.drawText("Time: "+(time/60)+":"+(time%60)+"  Calories: "+calories+"  Distance: "+distance+"KM\n"+"Speed: "+speed+"KM/H   Max Speed: "+maxSpeed+"KM/H", 8, 14, paint);
    }
}

public void setStats(long time, float distance, float speed, float maxSpeed, double calories)
{
    this.time = time/1000;
    this.distance = distance;
    this.speed = speed;
    this.maxSpeed = maxSpeed;
    this.calories = calories;

}
  }

它根本不会进入绘制方法。

这是来自调用叠加层的代码的片段。

public class Begin_Run_Map extends MapActivity implements LocationListener 
{

   //Other variables declared here for calculations

   //Overlay variables
   MyLocationOverlay myLocOver;
StatsOverlay statsOverlay;

public void onCreate(Bundle savedStateInstance)
{
    super.onCreate(savedStateInstance);
    setContentView(R.layout.race_run);

            //Setting up of the mapview, initalising variables and setting up of location manager etc done here

            mapView = (MapView) findViewById(R.id.race);
    mapView.setSatellite(false);
    mapView.setBuiltInZoomControls(true);
    mapView.displayZoomControls(true);

    mapCon = mapView.getController();



     screenOverlays = mapView.getOverlays();

    myLocOver = new MyLocationOverlay(this,mapView);
    statsOverlay = new StatsOverlay();
    screenOverlays.add(statsOverlay);
    screenOverlays.add(myLocOver);
      }

onLocationChange(Location loc)方法中调用方法centerOnLocation(Location loc),将值传递给显示重叠类

  private void centerOnLocation(Location loc) 
{
    // TODO Auto-generated method stub
    double lat = loc.getLatitude();
    double lng = loc.getLongitude();

    GeoPoint me = new GeoPoint((int) (lat * 1E6),(int) (lng * 1E6));
    mapCon.animateTo(me);
    if(statsOverlay != null)
    {

        statsOverlay.setStats(times.get(times.size()-1), distance, avg_Speed, maxSpeed, caloriesBurned);
    }
}

MyLocationOverlay的其他叠加层以及显示用户之前点数的另一个叠加层正常工作。

感谢您的帮助

1 个答案:

答案 0 :(得分:1)

声明这样的绘制函数:

@Override
public boolean draw(Canvas canvas, MapView mv, boolean shadow, long when){
        super.draw(canvas, mv, shadow);
        //declare the paint object here
        Paint paint = new Paint();

}