简体   繁体   中英

Android Studio: How to get an ImageView to move in the direction of rotation

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.

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;

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(); 

    }

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));

is the code that adds/subtracts your x and y.

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.

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(); 

    }

Helpful links:

https://math.stackexchange.com/questions/142073/calculate-new-graph-point-with-coordinate-and-angle

Run loop every second java

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM