Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8466195
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T15:16:12+00:00 2026-06-10T15:16:12+00:00

I was studying live wallpaper. But first i have to study canvas animation. I

  • 0

I was studying live wallpaper. But first i have to study canvas animation. I have a code here that works ok. It uses a math.random(). But what i want is some like this.
enter image description here

Here i want the image to move just like the path illustrated above. My idea is to use math.random for x and y. But the problem of using that is the image will appear anywhere in the screen. I want the image, to create a random path. I know its about x and y coordinates but i cant think of how the path will trigger randomly? I cant actualy explain it. But if you know about the bouncing ball, the ball has a random corrdinates. Just like that. here is my code.

public class MainActivity extends Activity {

    private Game game;
     public Handler updateHandler = new Handler(){
            /** Gets called on every message that is received */
            // @Override
            public void handleMessage(Message msg) {
                game.update();
                game.invalidate();
                super.handleMessage(msg);
            }
        };
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        game = new Game(this);
        setContentView(game);

        Thread myThread = new Thread(new UpdateThread());
        myThread.start();
    }

    public class UpdateThread implements Runnable {

        @Override
        public void run() {

             while(true){
                 try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                 MainActivity.this.updateHandler.sendEmptyMessage(0);
            }
        }

    }

}


public class Game extends View {

    private Bitmap image;
    private Paint paint;
    private int x=0;

    public Game(Context context) {
        super(context);
        image = Bitmap.createBitmap(BitmapFactory.decodeResource(this.getResources(), R.drawable.ic_launcher));
        paint = new Paint();
    }

    // called every Frame
    protected void onDraw(Canvas canvas) {

        canvas.drawBitmap(image, x, 0, paint);
    }

    // called by thread
    public void update() {
            x = 1;
            x += Math.random() * 100;

    }

}

I hope you can help me here.. Thank you.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T15:16:14+00:00Added an answer on June 10, 2026 at 3:16 pm

    Here’s code for endless linear interpolation loop (you might want to change it to smooth curves later on);

    PointF mImagePos = new PointF();
    PointF mImageSource = new PointF();
    PointF mImageTarget = new PointF();
    long mInterpolateTime;
    
    protected void onDraw(Canvas canvas) {
        canvas.drawBitmap(image, mImagePos.x, mImagePos.y, paint);
    }
    
    public void update() {
        final long INTERPOLATION_LENGTH = 2000;
        long time = SystemClock.uptimeMillis();
        if (time - mInterpolateTime > INTERPOLATION_LENGTH) {
            mImageSource.set(mImageTarget);
            mImageTarget.x = (float)(Math.random() * 100);
            mImageTarget.y = (float)(Math.random() * 100);
            mInterpolateTime = time;
        }
    
        float t = (float)(time - mInterpolateTime) / INTERPOLATION_LENGTH;
        // For some smoothness uncomment this line;
        // t = t * t * (3 - 2 * t);
    
        mImagePox.x = mImageSource.x + (mImageTarget.x - mImageSource.x) * t;
        mImagePos.y = mImageSource.y + (mImageTarget.y - mImageSource.y) * t;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Studying STL I have tried to negate a functor with not2 but encontered problems.
i studying TDateTime functions and procedure, but not found something that allow me to
After studying Miles Jones cupcake forum plugin, I have a couple of questions here:
I'm in University studying computer science. This is actually my first big assignment but
I have been studying testOnDemandRTSPServer code and I want to do streaming by taking
Our company is studying the adoption of a new SaaS, but we'll have to
Complex question I assume, but studying OWL opened a new perspective to live, the
when studying RMI sometimes (head first Java) dudes use Naming.rebind(name, object) but other peoples
Studying Haskell, i'm trying to code a function takeIf that given a condition and
Studying asp.net mvc 3 + EF code-first. I am new to both. My example

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.