谢谢回复。我几乎已经放弃了让它以我认为应该的方式工作。当然,我认为事情应该如何发生并不是它们确实如何发生。:)
这基本上是它的工作原理,但在某些情况下它似乎偏离了一个像素,当事物落在我尚未弄清楚的某些边界条件上时,圆圈似乎缺少部分。就我个人而言,我认为在应用程序代码中这是不可接受的,并且应该在 Android 库中......眨眼眨眼,如果你为 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);
}
...
}