Android的画布坐标系 [英] Android Canvas Coordinate System

查看:335
本文介绍了Android的画布坐标系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找到如何改变坐标系统画布信息。我有我想提请使用像圆和线帆布一些矢量数据,但数据的坐标系统不匹配画布坐标系。

I'm trying to find information on how to change the coordinate system for the canvas. I have some vector data I'd like to draw to a canvas using things like circles and lines, but the data's coordinate system doesn't match the canvas coordinate system.

有没有办法我使用到屏幕的单位?

我画到未占用整个显示一个ImageView的。

I'm drawing to an ImageView which isn't taking up the entire display.

如果我有之前每个绘图调用,如何找到我的ImageView的宽度和高度,做我自己的计算?

的的getWidth()和getHeight()调用我试图似乎返回整个画布的大小,而不是它是没有帮助的ImageView的的大小。

The getWidth() and getHeight() calls I tried seem to be returning the entire canvas size and not the size of the ImageView which isn't helpful.

我看到一些东西矩阵,是一些能够为我工作?

我试图用公共空间规模(浮点SX,SY浮动),但更像一个像素级变焦,而不是通过扩大每个像素的矢量规模的功能。这意味着如果尺寸增大以适应屏幕,线厚度也增加。

I tried to use the "public void scale(float sx, float sy)", but that works more like a pixel level zoom rather than a vector scale function by expanding each pixel. This means if the dimensions are increased to fit the screen, the line thickness is also increased.

更新:

一些研究,我开始认为有没有办法改变坐标系别的东西之后。我需要我的所有的坐标到屏幕上的像素坐标图和修改每个矢量这样做。该的getWidth()和getHeight()似乎是现在的工作对我来说更好。我能说什么是错的,但我怀疑我不能使用构造函数中这些方法。

After some research I'm starting to think there's no way to change coordinate systems to something else. I'll need to map all my coordinates to the screen's pixel coordinates and do so by modifying each vector. The getWidth() and getHeight() seem to be working better for me now. I can say what was wrong, but I suspect I can't use these methods inside the constructor.

推荐答案

感谢您的答复。我有pretty在得到这个在我觉得应该在上班的路上多放弃。当然,我是怎么想的事情应该发生的不是他们怎么发生的。 :)

Thanks for the reply. I have pretty much given up on getting this to work in the way I think it should. Of course how I think things should happen isn't how they do happen. :)

下面基本上它的工作原理,但似乎在某些情况下,像素为关,而圈似乎缺少的部分,当事情上的一些边界条件,我有土地没有搞清楚。我个人认为这是不能接受的是里面的应用程序code和应该是在Android的图书​​馆...卿卿我我,轻移如果为谷歌工作的微调。 :)

Here's basically how it works, but it seems to be off by a pixel in some cases and the circles seem to be missing sections when things land on some boundary conditions I have yet to figure out. Personally I think this is unacceptable to be inside application code and should be in the Android libraries... wink wink, nudge nudge if you work for Google. :)

private class LinearMapCanvas
{
    private final Canvas canvas_; // hold a wrapper to the actual canvas.

    // scaling and translating values:
    private double scale_;

    private int translateX_;
    private int translateY_;

// linear mapping from my coordinate system to the display's:
    private double mapX(final double x)
    {
    final double result = translateX_ + scale_*x;
    return result;
    }

    private double mapY(final double y)
    {
        final double result = translateY_ - scale_*y;
        return result;
    }

    public LinearMapCanvas(final Canvas canvas)
    {
        canvas_ = canvas;

// Find the extents of your drawing coordinates:
        final double minX = extentArray_[0];
    final double minY = extentArray_[1];
    final double maxX = extentArray_[2];
    final double maxY = extentArray_[3];

// deltas:
    final double dx = maxX - minX;
    final double dy = maxY - minY;

// The display's available pixels, accounting for margins and pen stroke width:
    final int width = width_ - strokeWidth_ - 2*margin_;
    final int height = height_ - strokeWidth_ - 2*margin_;

    final double scaleX = width / dx;
    final double scaleY = height / dy;

    scale_ = Math.min(scaleX , scaleY); // Pick the worst case, so the drawing fits

// Translate so the image is centered:
        translateX_ = (int)((width_ - (int)(scale_*dx))/2.0 - scale_*minX);
        translateY_ = (int)((height_ - (int)(scale_*dy))/2.0 + scale_*maxY);
    }

// wrappers around the canvas functions you use.  These are only two of many that would need to be wrapped.  Annoying and error prone, but beats any alternative I can find.
    public void drawCircle(final float cx, final float cy, final float radius, final Paint paint)
    {
    canvas_.drawCircle((float)mapX(cx), (float)mapY(cy), (float)(scale_*radius), paint);
    }

    public void drawLine(final float startX, final float startY, final float stopX, final float stopY, final Paint paint)
    {
    canvas_.drawLine((float)mapX(startX), (float)mapY(startY), (float)mapX(stopX), (float)mapY(stopY), paint);
    }
...
}

这篇关于Android的画布坐标系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆