【问题标题】:draw line between 2 distance points on android在android上的2个距离点之间画线
【发布时间】:2012-02-17 22:30:28
【问题描述】:

假设我们有一个 400x800 的画布,我想画一条连接点的线 P1 (10,10) 和 P2(500000,800000)。 如您所见,第二个点远在画布边界之外。如果我使用 canvas.darwLine(p1.x, p1.y,p2.x,p2.y,paint) 应用程序冻结并且应用程序变得不可用。 使用剪裁并不能解决问题,绘图引擎仍在尝试将像素一直绘制到第二个点

有什么建议或解决方法吗?

【问题讨论】:

  • 如果您的 point2 超出 400x800 的范围,请将 point 2 更改为 P2(400,800)
  • 我无法测试这个,因为我不在我的开发机器上,但你有没有尝试过以下组合:canvas.clipRect(),在路径上画线,然后使用canvas.drawPath()

标签: java android drawing android-canvas


【解决方案1】:

如果 P2 在可见区域之外(在这种特殊情况下为 480x800),则计算这条线与边框的交点,然后使用交点代替 P2。

【讨论】:

  • 谢谢彼得,我希望可以在 Canvas 本身上执行一些操作,以防止不必要的绘图。数学时间:)
【解决方案2】:

您可以通过以下方式缩小生产线:

int maxX = 400;
int maxY = 800;

//Calculate how much we have to scale down to fit in the bounds:
float scaleX = (maxX - p1.x)/p2.x;
float scaleY = (maxY - p1.y)/p2.y;

//Get the smallest scale, so that we fit in both axises.
float scale = Math.min(scaleX, scaleY);

//Only scale if we are scaling down. There is no need to make lines smaller than the screen scale up to the screen bounds:
if(scale < 1.0f){
    p2.x *= scale;
    p2.y *= scale;
}

我还没有尝试过,所以我不能保证它会起作用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-11
    • 2022-01-07
    • 2012-07-17
    • 2017-07-06
    • 1970-01-01
    相关资源
    最近更新 更多