Garmaine Staff asked 2 years ago

I am trying to put an ImageView between 2 moving ball objects. Using a path drawer is not possible since I try to avoid the OnDraw method because of its limitations(60 refreshes/second). The 2 ball objects are continuously moving on the screen (but always with the same distance from each other). Since the x and y coordinates are always changing, it would be nice to set the endpoints of the 'stick' object to the 2 positions where the 2 balls are standing in that specific scenario, and of course, when they have moved, reset the stick between the new endpoints.

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ball = (ImageView) findViewById(R.id.ball);
    ball2 = (ImageView) findViewById(R.id.ball2) ;

    widthMiddle = getWindowManager().getDefaultDisplay().getWidth() / 2;
    heightMiddle = getWindowManager().getDefaultDisplay().getHeight() / 2;

    timer.schedule(new TimerTask() {
        @Override
        public void run() {
            handler.post(new Runnable() {
                @Override
                public void run() {
                    x1 = widthMiddle + r1 * (float)Math.sin(Math.toRadians(a1));
                    y1 = heightMiddle + r1 * (float)Math.cos(Math.toRadians(a1));
                    x2 = x1 + r2 * (float)Math.sin(Math.toRadians(a2));
                    y2 = y1 + r2 * (float)Math.cos(Math.toRadians(a2));
                    a1 += 1.0;
                    a2 -= 2.0;
                    draw();
                }
            });
        }
    }, 0, 3);
}
public void draw(){
    ball.setX(x1);
    ball.setY(y1);
    ball2.setX(x2);
    ball2.setY(y2);
}

Now for the testing purposes, the ball1 object is swinging around the midpoint of the screen, and the ball2 is just doing the same around the ball1 object.