Android中计算屏幕上两点之间的路径
起初问题对我来说似乎很简单,但现在我陷入了困境。
场景 我想在屏幕上移动图像,沿着我创建的特定路径。移动该图像是在线程上进行的,类似于:
@Override
public void run() {
Canvas c;
while (run) {
c = null;
try {
c = panel.getHolder().lockCanvas(null);
synchronized (panel.getHolder()) {
panel.updateImageCoordinates();
panel.onDraw(c);
}
} finally {
if (c != null) {
panel.getHolder().unlockCanvasAndPost(c);
}
}
}
对于我想要移动的图像,我有一个列表,其中包含它应该移动的要点。每个坐标有:
public class Coordinates {
private int x = 0;
private int y = 0;
private int speedX=0;
private int speedY=0;
}
例如,我的第一个点是-5;-30,我需要到达第二个点50.50。绘制图像的下一个坐标的计算是在 updateImageCooperatives() 上进行的。我的问题是我不知道如何计算 speedX 和 speedY 以便我沿直线从 A 点到达 B 点。基本上,对于 updateImageCoorindates() 的每次执行,我需要做的事情:
image.currentX= image.currentX+speedX;
image.currentY= image.currentY+speedY
//Check if I reached the B point. if so, move to next point.
我不知道基于坐标,如何计算 x 和 Y 方向上的速度。
我附上一张图片作为示例。任何帮助表示赞赏。
Problem seemed very simple to me at first but now I am stuck.
Scenario
I want to move a image on the screen, on a certain path I create. Moving this image is being made on a thread, something like:
@Override
public void run() {
Canvas c;
while (run) {
c = null;
try {
c = panel.getHolder().lockCanvas(null);
synchronized (panel.getHolder()) {
panel.updateImageCoordinates();
panel.onDraw(c);
}
} finally {
if (c != null) {
panel.getHolder().unlockCanvasAndPost(c);
}
}
}
for the Image I want to move I have a List with main points where it should go. Each coordinate has:
public class Coordinates {
private int x = 0;
private int y = 0;
private int speedX=0;
private int speedY=0;
}
For example, my first point is -5;-30 and I need to get to second point 50.50. The calculation of next coordinates to draw the image is made on updateImageCoordinates(). My problem is that I don't know how to calculate speedX and speedY so that I get from point A to point B on a straight line. Basically for each execution of updateImageCoorindates() I need to do:
image.currentX= image.currentX+speedX;
image.currentY= image.currentY+speedY
//Check if I reached the B point. if so, move to next point.
I don't know based on knowing the coordinates, how I can calculate the speed on x and Y directions.
I attach a image for exemplification. Any help is appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不确定我是否清楚地理解了你的问题......
如果你正在寻找将 PointA 转换为 AB 线上的点的函数。
包含两个点的线将具有方程:
-30 = -5*a + b
和
50 = 50*a + b
所以
b = -250/11
a = 16/11
因此要找到下一个点,您必须:
检查下一个点的 x 是否位于目标点的左侧 (-1) 或右侧 (+1),
并通过以下方式计算下一个点:
I'm not shure if I've understood your question clearly...
If you are looking for function which will translate PointA into point on line A-B.
Line containing both points will have equation:
-30 = -5*a + b
and
50 = 50*a + b
so
b = -250/11
a = 16/11
so to find next point you have to:
check if x of next point is at left (-1) or right (+1) of the destination point
and calculate next point by:
您将找到 API 演示我认为对于动画很有用。特别是,请查看 自定义评估器。
You'll find the API Demos for Animation useful I think. In particular, check out Custom Evaluator.