Issue
I am attempting to draw a custom Drawable overlay on a MapView. I use a custom class that extends Drawable to draw the circle on a canvas. Then I pass a radius variable to it and add it to an ItemizedOverlay which gets applied to the MapView.
So far it is working out pretty well, but for some reason, the canvas.drawCircle method is drawing a transparent circle with what looks like an ellipse inside of it, and I can't, for the life of me, figure out what is causing it. I do want the circle to be transparent, but I don't want this ellipse thing inside of it.
Here is the custom Drawable class:
import android.graphics.Canvas;
import android.graphics.ColorFilter;
import android.graphics.Paint;
import android.graphics.drawable.Drawable;
public class PileDrawable extends Drawable {
private static final Paint paint = new Paint();
int rad;
public PileDrawable(int radius) {
paint.setARGB(100, 0, 255, 0);
paint.setAntiAlias(false);
paint.setStyle(Paint.Style.FILL);
rad=radius;
}
@Override
public void draw(Canvas canvas) {
canvas.drawCircle(0, 0, rad, paint);
}
@Override
public int getOpacity() {
return 0;
}
@Override
public void setAlpha(int alpha) {
}
@Override
public void setColorFilter(ColorFilter cf) {
}
}
This is kind of what it looks like, ignore my lack of MS paint skills...
http://img222.imageshack.us/img222/4356/circleellipse.png
I have a lingering feeling that the problem occurs when I apply the bounds to the Drawable.
PileDrawable pd = new PileDrawable((int)(mapView.getProjection().metersToEquatorPixels(Radius) * (1/ Math.cos(Math.toRadians(point.getLatitudeE6())))));
pd.setBounds(0,0,pd.getIntrinsicWidth(),pd.getIntrinsicHeight()); //HERE?
So, any ideas?
Solution
it might be a little late, but here is how i draw a circle on the map as an overlay. hope this helps people in the future
@Override
public void draw(Canvas canvas, MapView mapView, boolean shadow)
{
Projection proj = mapView.getProjection();
GeoPoint loc = new GeoPoint(geo.getLat(), geo.getLng());
Point point = new Point();
proj.toPixels(loc, point);
float rad = (float) (proj.metersToEquatorPixels(radius) * (1 / Math.cos(Math.toRadians(loc.getLatitudeE6() / 1000000))));
Paint circle = new Paint();
circle.setColor(colour);
circle.setAlpha(30);
circle.setAntiAlias(true);
circle.setStyle(Style.FILL);
canvas.drawCircle(point.x, point.y, rad, circle);
super.draw(canvas, mapView, shadow);
}
and works like a charm. i am not sure as to why the ellipse is drawn since there is nothing in the code to draw a ellipse, but i am guessing it could be cause of the integer value conversion rather than the float value conversion, while u initialize the PileDrawable
Answered By - vivek86
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.