Android:canvas.drawCircle 里面有一个椭圆?

发布于 2024-11-02 00:38:42 字数 1563 浏览 3 评论 0原文

我正在尝试在 MapView 上绘制自定义 Drawable 叠加层。 我使用扩展 Drawable 的自定义类在画布上绘制圆圈。然后我将一个半径变量传递给它,并将其添加到应用于 MapView 的 ItemizedOverlay 中。

到目前为止,它运行得很好,但由于某种原因,canvas.drawCircle 方法正在绘制一个透明的圆圈,其内部看起来像一个椭圆形,而我一生都无法弄清楚它是什么造成它。我确实希望圆圈是透明的,但我不希望其中有椭圆形的东西。

这是自定义的 Drawable 类:

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) {

    }
}

这就是它的样子,忽略我缺乏 MS 绘画技能...

http://img222.imageshack.us/img222/4356/circleellipse.png

我有一种挥之不去的感觉,问题发生在我将边界应用于可绘制对象。

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?

那么,有什么想法吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

荆棘i 2024-11-09 00:38:42

可能有点晚了,但这是我在地图上画一个圆圈作为叠加层的方法。希望这对未来的人们有所帮助

@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);

}

,并且像魅力一样发挥作用。我不确定为什么要绘制椭圆,因为代码中没有任何内容可以绘制椭圆,但我猜测这可能是在初始化 PileDrawable 时进行整数值转换而不是浮点值转换的原因

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文