机器人工作室:如何获得的ImageView在旋转方向移动 [英] Android Studio: How to get an ImageView to move in the direction of rotation
问题描述
这个问题一直停留在我的脑海了一段时间,和IM想知道如果我在想念一个方法或something.Im试图找出我怎么能在Android的工作室(IDE)获得的ImageView中的方向移动的旋转,虽然这可能是形象最好的解释我的问题
This question has been stuck on my mind for a while, and im wondering if im missing a method or something.Im trying to figure out how i can get an imageview in Android Studio(IDE) to move in the direction of its rotation, though this image probably best explains my question
推荐答案
所以,我发现了如何与一些链接的帮助下做到这一点,我提出我自己的解决方案。
So I found out how to do it with the help of some links and i formulated my own solution.
首先,我计算我多少增加/使用其现有的一些触发旋转图像的X和Y减去。两者之间的比例是什么赋予的方向移动的效果。
First, I calculate how much I am adding/subtracting from the image's x and y using its current rotation with some Trig. The ratio between the two is what gives the effect of moving in the direction.
final double rise = Math.toDegrees( Math.sin(toRadians(YOU.getRotation())) )*SPEED;
final double run = Math.toDegrees( Math.cos(toRadians(YOU.getRotation())) )*SPEED;
然后,我用一个定时循环并添加一组x和y
Then, I use a timed loop and add the set x and y
(new Thread(new Runnable()
{
@Override
public void run()
{
for (int x=0; x<600; x++) {
try {
Thread.sleep(6);
runOnUiThread(new Runnable() thread
{
@Override
public void run() {
YOU.setX((float) (YOU.getX() + run)); //pay attention here
YOU.setY((float) (YOU.getY() + rise));//pay attention here
}
});
} catch (InterruptedException e) {
// ooops
}
}
}
})).start();
}
注意我是如何使用for循环与一群其他code,应该指出的是,这个心不是neccesary,我却用它为我的特定需求。你可以把一个while循环,而不是一个for循环如果u想,但这个部分
Notice how I use a for loop with a bunch other code, It should be noted that this isnt neccesary, but I use it for my specific needs. You can put a while loop instead of a for loop if u want but this part
YOU.setX((float) (YOU.getX() + run));
YOU.setY((float) (YOU.getY() + rise));
在code,增加/减去你的x和y。
is the code that adds/subtracts your x and y.
另外,变量速度
在的声明上涨
和运行
设定为0.02(这里没有显示),因为我经历了一个循环,使它看起来更平滑添加x和y。越大这里数越去意大利里拉(每个时代U增加),但它不影响角度的准确性前进。虽然我reccomend将至少填充数量那里。
Also, the variable SPEED
in the declaration of rise
and run
is set to 0.02 (not shown here) because I add x and y through a loop that makes it look much smoother. The greater the number here the farther itl go (per time u add) but it doesnt affect the accuracy of angle forward. Though I'd reccomend putting at least a filler number there.
这是我原来的code都在一个地方如果你需要它。
Here is my original code all in one place in case you need it.
public void moveloop(){
final double rise = Math.toDegrees( Math.sin(toRadians(YOU.getRotation())) )*SPEED;
final double run = Math.toDegrees( Math.cos(toRadians(YOU.getRotation())) )*SPEED;
(new Thread(new Runnable()
{
@Override
public void run()
{
for (int x=0; x<600; x++) {
try {
Thread.sleep(6);
runOnUiThread(new Runnable() thread
{
@Override
public void run() {
YOU.setX((float) (YOU.getX() + run));
YOU.setY((float) (YOU.getY() + rise));
}
});
} catch (InterruptedException e) {
// ooops
}
}
}
})).start();
}
有用的链接:
<一个href=\"http://math.stackexchange.com/questions/142073/calculate-new-graph-point-with-coordinate-and-angle\">http://math.stackexchange.com/questions/142073/calculate-new-graph-point-with-coordinate-and-angle
这篇关于机器人工作室:如何获得的ImageView在旋转方向移动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!