• Skip to main content
  • Skip to primary sidebar
  • Skip to footer
  • About
  • Life
  • Tech
  • Travel
  • Work
  • Questions
  • Contact

Welcome

.

Android: Position image between 2 coordinates

April 11, 2020 by

Questions › Android: Position image between 2 coordinates
0
Vote Up
Vote Down
Garmaine asked 3 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.

Are you looking for the answer?
Original Question and Possible Answers can be found on `http://stackoverflow.com`

Question Tags: android, imageview, java

Please login or Register to submit your answer




Primary Sidebar

Tags

Advancements architecture beautiful life best building calling city commercial convenience employment Finances Cognitive decline Future gadgets Hidden Gems highway Home houses hydration Impact Innovations lamp lighting Mental health military tech Must-See New York City occupation Productivity recreation romance sepia shopping sippy cups smartphones social Technological breakthroughs technology toddlers Treasures turns Uncover Well-being Wonders Work Young onset dementia

Newsletter

Complete the form below, and we'll send you all the latest news.

Footer

Footer Funnies

Who knew that reading the footer could be such a hilarious adventure? As we navigate websites, books, and documents, we often stumble upon the unassuming space at the bottom, only to discover a treasure trove of amusement. In this side-splitting compilation, we present 100 jokes that celebrate the unsung hero of content – the footer. Get ready to chuckle, giggle, and maybe even snort as we dive into the world of footnotes, disclaimers, and hidden comedic gems. Brace yourself for a wild ride through the footer!

Recent

  • Unveiling the Enigma: Almost-Magical Lamp Lights Highway Turns
  • The Impact of Young Onset Dementia on Employment and Finances: Optimizing Post-Diagnostic Approaches
  • 11 Wonders of 2023 Technological Breakthrough – Unveiling the Future
  • Work from Home and Stay Mentally Sane – Achieve Productivity and Well-being
  • Hidden Gems of New York City – Uncover the Must-See Treasures!

Search

Tags

Advancements architecture beautiful life best building calling city commercial convenience employment Finances Cognitive decline Future gadgets Hidden Gems highway Home houses hydration Impact Innovations lamp lighting Mental health military tech Must-See New York City occupation Productivity recreation romance sepia shopping sippy cups smartphones social Technological breakthroughs technology toddlers Treasures turns Uncover Well-being Wonders Work Young onset dementia

Copyright © 2023