1

How do you move an control (for example a ImageView) in a certain direcion in degrees. There is no coordinate where the control needs to stop moving. we want to move it in a direction in degrees (0-360)

This doesn't work:

RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);

        params.setMargins(100,75, 0,0);
        params.topMargin= 100;
John Smith
  • 53
  • 2
  • 9
  • Follow this : http://stackoverflow.com/questions/8981845/androidrotate-image-in-imageview-by-an-angle – YuDroid Mar 30 '15 at 10:09
  • I think you misunderstood my question. I don't want to rotate the imageview but i want to move it diagonally across my screen – John Smith Mar 30 '15 at 10:10
  • Then you should explain it in detail. You can take help of `Matrix`. More info here : http://stackoverflow.com/questions/18735280/android-rotate-image-in-imageview-by-90degrees-but-without-delay – YuDroid Mar 30 '15 at 10:11
  • The article you posted is not related to my problem. I want to MOVE it, not ROTATE – John Smith Mar 30 '15 at 10:15
  • Thats Translation check this http://stackoverflow.com/questions/10276251/how-to-animate-a-view-with-translate-animation-in-android – Hardik Trivedi Mar 30 '15 at 10:21
  • http://developer.android.com/reference/android/graphics/Matrix.html#setTranslate(float, float). You can also MOVE it using Matrix. – YuDroid Mar 30 '15 at 10:22
  • Thanks for these articles, We are close now ;]. However we are running into a problem: These kind of "movements/translations" only work if you know the endpoint. However we just want to move an imageview in a certain direction based on degrees. (for example 244 degrees, and it keeps moving forever in that direction, without endpoint) – John Smith Mar 30 '15 at 10:37
  • You could set the endpoint outside the screen, which would for almost all purposes have the desired effect – Soana Mar 30 '15 at 11:07
  • But we don't know the endpoint. We only have a start point, a direction to move (0-360 degrees) and a movement speed. – John Smith Mar 30 '15 at 12:10
  • Well, you know the dimensions of the screen, the dimensions of the picture and the direction, so you know all the things you need to calculate an endpoint outside of the screen. – Soana Mar 30 '15 at 12:19
  • Okay nice! can you push me in a direction how i can calculate this? – John Smith Mar 30 '15 at 12:28
  • I had a similar question which I found the solution to. Heres the link if it helps.http://stackoverflow.com/questions/31359371/android-studio-how-to-get-an-imageview-to-move-in-the-direction-of-rotation/31684355#31684355 – Mazino Jul 28 '15 at 18:30

1 Answers1

0

Found the solution, this is it: Sinus was the solution...

RelativeLayout root = (RelativeLayout) findViewById( R.id.rootLayout );
int originalPos[] = new int[2];
bal.getLocationOnScreen( originalPos );

Double sin = (Math.sin(Math.toRadians(degrees)) * (root.getHeight() / 2 )) / Math.sin(Math.toRadians(90 - degrees));
John Smith
  • 53
  • 2
  • 9