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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T06:32:45+00:00 2026-05-23T06:32:45+00:00

I currently have (among others) these classes: public class Main extends Activity { Panel

  • 0

I currently have (among others) these classes:

public class Main extends Activity {

Panel p;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    p = new Panel(this);
    setContentView(p);
    }

@Override
public boolean onTouchEvent(MotionEvent event) {
    p.onTouchEvent(event);
    // Make your UI thread sleep.
    try {
        Thread.sleep(30);
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return true;


}

and

public class Panel extends SurfaceView implements SurfaceHolder.Callback {

private ViewThread mThread;
private ArrayList<GraphicsElement> mElements = new ArrayList<GraphicsElement>();
public static int panelHeight;
public static int panelWidth;
private int numberOfElements = 0;
private Paint mPaint;

public Panel(Context context) {
    super(context);
    getHolder().addCallback(this);
    mThread = new ViewThread(this);
    mPaint = new Paint();
    mPaint.setColor(Color.CYAN);
    mPaint.setTextSize(20);
}

public void doDraw(long elapsed, Canvas canvas) {
    canvas.drawColor(Color.parseColor("#003045"));
    if (!(mElements.size() > 15)) {
        synchronized (mElements) {
            for (GraphicsElement element : mElements) {
                element.doDraw(canvas);
            }
            canvas.drawText("FPS: " + Math.round(1000f / elapsed) + " Elements: " + numberOfElements, 10, 30, mPaint);
        }
    } else {
        mElements.clear();
        numberOfElements = 0;
    }
}

public void animate(long elapsedTime) {
    synchronized (mElements) {
        for (GraphicsElement element : mElements) {
            element.animate(elapsedTime);
        }
    }
}

@Override
public boolean onTouchEvent(MotionEvent event) {
    int action = event.getAction();
    int xspeed = 0;
    int yspeed = 0;

    if (action == MotionEvent.ACTION_DOWN || action == MotionEvent.ACTION_MOVE) {

        if (event.getX() > panelWidth / 2) {
            xspeed = 5;
        } else if (event.getX() < panelWidth / 2) {
            xspeed = -5;
        }

        synchronized (mElements) {
            for (GraphicsElement element : mElements) {
                element.changeSpeed(xspeed, yspeed);
            }
        }
    }
    return true;
}

@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

@Override
public void surfaceCreated(SurfaceHolder holder) {
    if (!mThread.isAlive()) {
        mThread = new ViewThread(this);
        mThread.setRunning(true);
        mThread.start();
    }
    mElements.add(new GraphicsElement(getResources(), 80, 300));
    numberOfElements += 1;
}

public void surfaceDestroyed(SurfaceHolder holder) {

I also have ViewThread, just my animation thread, and GraphicsElement, which defines the moving object. My animation is going very slow(on touch), and I think it has something to do with my .sleep() method. Could anyone please help me ?

Edit: I’m using .sleep() because I don’t want to flood TouchEvents.
i’m trying to get it like: Check for TouchEvent, Sleep, Check for TouchEvent, Sleep.. etc…

  • 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-05-23T06:32:46+00:00Added an answer on May 23, 2026 at 6:32 am

    I’ve had the same issues with touch slowing things down and ended up with a similar approach to yours (sleeping the UI thread on touch events, as recommended by a Google game developer). The thing that seemed to help me was playing with the length of the sleep time. Try increasing it to 70 ms or even more and see if that helps. Most apps don’t need a super high sample rate for touch input.

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

Sidebar

Related Questions

I currently have a class and I'm trying to create an easy GUI to
Currently I'm using a base class for some Customer model classes. As defined as
I have Camera class, which handles camera behavior. Among it's fields is a reference
I currently have a bash application that, among other things, uses cURL to upload
I'm currently working with a JQuery Plugin called Raty (among many others), as normal
I have a domain model that includes something like this: public class Customer :
My current setup : I have an entity object with some properties, among them
I currently have an MS Access application that connects to a PostgreSQL database via
I currently have speakers set up both in my office and in my living
I currently have an existing database and I am using the LINQtoSQL generator tool

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.