![](/img/trans.png)
[英]how to add two rotation with different pivots on a imageView in android studio?
[英]Android Studio: How to get an ImageView to move in the direction of rotation
這個問題一直困擾着我一段時間,我想知道我是否缺少一種方法或某種東西。我試圖弄清楚如何在Android Studio(IDE)中獲得圖像視圖以沿其旋轉方向移動此圖像可能最好地解釋了我的問題
因此,我找到了一些鏈接的幫助,並制定了自己的解決方案。
首先,我使用一些Trig使用當前旋轉來計算要從圖像的x和y中減去/減去多少。 兩者之間的比例是產生方向移動效果的原因。
final double rise = Math.toDegrees( Math.sin(toRadians(YOU.getRotation())) )*SPEED;
final double run = Math.toDegrees( Math.cos(toRadians(YOU.getRotation())) )*SPEED;
然后,我使用定時循環並添加x和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循環與其他代碼結合使用的,應該注意,這不是必需的,但是我將其用於我的特定需求。 如果您願意,可以放一個while循環而不是for循環,但這部分
YOU.setX((float) (YOU.getX() + run));
YOU.setY((float) (YOU.getY() + rise));
是加/減x和y的代碼。
另外,在rise
和run
聲明中的變量SPEED
設置為0.02(此處未顯示),因為我通過一個循環添加了x和y,這使它看起來更加平滑。 這里的數字越大(每次添加),它就越遠,但它不會影響前角的精度。 盡管我建議至少在其中添加一個補碼。
這是我的原始代碼,全部放在一個地方,以備您需要時使用。
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();
}
有用的網址:
https://math.stackexchange.com/questions/142073/calculate-new-graph-point-with-coordinate-and-angle
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.