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 9316613
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T02:39:55+00:00 2026-06-19T02:39:55+00:00

I want to make a view that show as if a point/circle is moving

  • 0

I want to make a view that show as if a point/circle is moving horizontally on the screen.
I want to achieve this only through Custom view.
So i wrote a piece of code in which i want to call onDraw() which will draw a circle, then it will call a handler, which will wait for 500ms and call onDraw(). So each time onDraw() is called circle will be shifted few steps towards right.
but here i am not able to syncup call between run() and onDraw().
code:

package com.example.layoutpractice;

import javax.net.ssl.HandshakeCompletedListener;

import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.PorterDuff.Mode;
import android.os.Build;
import android.os.Handler;
import android.util.Log;
import android.view.Display;
import android.view.View;
import android.view.WindowManager;

public class DrawView extends View {
    private static final String TAG = "DrawView";
    Context ctx = null;
    int MAX_WIDTH = 0;
    int MAX_HEIGHT = 0;
    int current_x = 0;
    int current_y = 0;
    Paint paint = null;

    public DrawView(Context context) {

    @TargetApi(13)
    private void initialize() {
        // get the screen size
        WindowManager wm = (WindowManager) ctx
                .getSystemService(Context.WINDOW_SERVICE);
        Display display = wm.getDefaultDisplay();
        Point size = new Point();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
            display.getSize(size);
            MAX_WIDTH = size.x;
            MAX_HEIGHT = size.y;
        } else {
            MAX_WIDTH = display.getWidth();
            MAX_HEIGHT = display.getHeight();
        }

        // initialize paint
        paint = new Paint();
        paint.setColor(Color.BLUE);
        this.setFocusable(true);
        this.setFocusableInTouchMode(true);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        Log.d(TAG, "onDraw()");
        current_y = MAX_HEIGHT / 2;
        while (current_x < MAX_WIDTH) {
            canvas.drawColor(Color.WHITE, Mode.CLEAR);
            canvas.drawCircle(current_x, current_y, 2, paint);
            current_x = current_x + 10;
            // new Thread(new TimeHandler()).start();
            new TimeHandler().execute();
        }
        super.onDraw(canvas);
    }

    class TimeHandler extends Handler {
        Runnable myRun = new Runnable() {
            public void run() {
                Log.d(TAG, "run");
                invalidate();
            }
        };

        public void execute() {
            this.postDelayed(myRun, 500);
        }

    }
}

logs

02-17 01:40:28.355: D/DrawView(1162): onDraw()
02-17 01:40:28.875: D/DrawView(1162): run
02-17 01:40:28.875: D/DrawView(1162): run
02-17 01:40:28.875: D/DrawView(1162): run
02-17 01:40:28.875: D/DrawView(1162): run
02-17 01:40:28.966: D/DrawView(1162): onDraw()
02-17 01:40:28.966: D/DrawView(1162): run
02-17 01:40:28.966: D/DrawView(1162): run
02-17 01:40:28.966: D/DrawView(1162): run
02-17 01:40:28.966: D/DrawView(1162): run
02-17 01:40:29.005: D/DrawView(1162): run
02-17 01:40:29.005: D/DrawView(1162): run
02-17 01:40:29.005: D/DrawView(1162): run
02-17 01:40:29.005: D/DrawView(1162): run
02-17 01:40:29.005: D/DrawView(1162): run
02-17 01:40:29.005: D/DrawView(1162): run
02-17 01:40:29.036: D/DrawView(1162): run
02-17 01:40:29.036: D/DrawView(1162): run
02-17 01:40:29.036: D/DrawView(1162): run
02-17 01:40:29.036: D/DrawView(1162): run
02-17 01:40:29.036: D/DrawView(1162): run
02-17 01:40:29.036: D/DrawView(1162): run
02-17 01:40:29.065: D/DrawView(1162): run
02-17 01:40:29.065: D/DrawView(1162): run
02-17 01:40:29.075: D/DrawView(1162): run
02-17 01:40:29.075: D/DrawView(1162): run
02-17 01:40:29.101: D/DrawView(1162): run
02-17 01:40:29.101: D/DrawView(1162): run
02-17 01:40:29.101: D/DrawView(1162): run
02-17 01:40:29.101: D/DrawView(1162): run
02-17 01:40:29.101: D/DrawView(1162): run
02-17 01:40:29.101: D/DrawView(1162): run
02-17 01:40:29.101: D/DrawView(1162): run
02-17 01:40:29.101: D/DrawView(1162): run
02-17 01:40:29.101: D/DrawView(1162): run
02-17 01:40:29.101: D/DrawView(1162): run
02-17 01:40:29.101: D/DrawView(1162): run
02-17 01:40:29.105: D/DrawView(1162): onDraw()
02-17 01:40:29.125: D/DrawView(1162): run
02-17 01:40:29.125: D/DrawView(1162): run
02-17 01:40:29.125: D/DrawView(1162): run
02-17 01:40:29.125: D/DrawView(1162): run
02-17 01:40:29.125: D/DrawView(1162): run
02-17 01:40:29.125: D/DrawView(1162): run
02-17 01:40:29.125: D/DrawView(1162): run
02-17 01:40:29.125: D/DrawView(1162): run
02-17 01:40:29.125: D/DrawView(1162): run
02-17 01:40:29.125: D/DrawView(1162): run
02-17 01:40:29.125: D/DrawView(1162): run
02-17 01:40:29.125: D/DrawView(1162): run
02-17 01:40:29.125: D/DrawView(1162): run
02-17 01:40:29.175: D/DrawView(1162): onDraw()
  • 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-19T02:39:56+00:00Added an answer on June 19, 2026 at 2:39 am

    Change

    while (current_x < MAX_WIDTH)
    

    to

    if (current_x < MAX_WIDTH)
    

    P.S. you design is wrong. onDraw is a special method, which can be called by android, not by you only. Every time onDraw is called by you via invalidate or by framework itself you schedule a runnable to run in 500ms. And you think that onDraw will be executed every 500ms (current_x = current_x + 10;)… you have a problem )

    To fix your problem, do not put any logic in ondraw. OnDraw should only draw view using current state. Leave only this:

        current_y = MAX_HEIGHT / 2;
        canvas.drawColor(Color.WHITE, Mode.CLEAR);
        canvas.drawCircle(current_x, current_y, 2, paint);
    
        super.onDraw(canvas);
    

    Create additional method in you custom View, which should do following:

    public void startBallMovement() {
          animationRunnable.run();
    }
    
    private Handler handler = new Handler();
    private final Rubbanle animationRunnable = new Runnable() {
         public void run() {
             current_x = current_x + 10;
             if (current_x < MAX_WIDTH) {
                 handler.postDelayed(animationRunnable, 500);
             }
             invalidate();
         }
    }
    

    I’m sure that you can use my code as is, but logic is clear.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to make an app that has a view that moves randomly or
I have a MS SQL view that I want to make available as a
Hi I want to make my textbox's width longer. The view that is generated,
I want to make a view in a database via Java that has a
I want to make a table view in my iPhone Application.. In that table
I want to make a table view in which i want to show some
Hey I want to make a view appear below the last that has been
I want to make a Views block's content show things that's related to the
I want to make a view where I can select multiple items from listview
I want to make a conversation view with ListBox, but I don't know how

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.