OnDraw does not contribute anything to the MapView

I am trying to draw a solid green rectangle in a canvas and then add that canvas to a MapView. However, after adding a "canvas" to the MapView, nothing appears in it. I look around but I cannot figure out what is wrong. I am using OSMDroid for MapView.

Here is the code for the class:

Public class DrawFrameLayout extends FrameLayout {

      MapView mapView;
GeoPoint geoPoint
Canvas  canvas = new Canvas();
private FrameLayout layout; 

public DrawFrameLayout(Context context, MapView mapView, GeoPoint geoPoint{
    super(context);

    layout = new FrameLayout(context);
    layout.setVisibility(VISIBLE);
    this.mapView = mapView;
    this.geoPoint= geoPoint
    setWillNotDraw(false);
    onDraw(canvas);

    FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    params.gravity = Gravity.NO_GRAVITY;

    addView(layout, params); 
}

@Override
public void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    Paint paint = new Paint();

    // draw a solid green rectangle
    paint.setStyle(Paint.Style.FILL); 
    paint.setColor(Color.GREEN);
    paint.setAntiAlias(true);
    canvas.drawRect(100, 5, 200, 30, paint);
}
}

      

This is how I instantiate the class from the main activity:

            DrawFrameLayout frameLayout = new DrawFrameLayout(mapView.getContext(), mapView, geoPoint);

        MapView.LayoutParams params = new MapView.LayoutParams(
                LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, geoPoint,
        MapView.LayoutParams.BOTTOM_CENTER, 0, 0);
        mapView.addView(frameLayout, params);
        frameLayout.setLayoutParams(params);
        frameLayout.setVisibility(View.VISIBLE);

      

+3


source to share





All Articles