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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T08:15:56+00:00 2026-06-14T08:15:56+00:00

I’ve been following this tutorial. I’ve been playing around with code, taking things out

  • 0

I’ve been following this tutorial. I’ve been playing around with code, taking things out I don’t need and adding things etc, but when I run it in the emulator the blocks move in a very stuttery motion. It’s like I’m getting a low FPS. For the first second or so of running the application it runs smoothly and then again for about a second when I recompile it before it closes. Any idea how to make it run smoothly all the time?

MainActivity.java

package com.example.mobilecoursework;

import android.app.Activity;
import android.os.Bundle;
import android.view.Window;

public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(new Blocks(this));
    }
}

blocks.java

package com.example.mobilecoursework;

import java.util.ArrayList;
import java.util.Random;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.view.SurfaceHolder;
 import android.view.SurfaceView;


 class Blocks extends SurfaceView implements SurfaceHolder.Callback {
    //
    private BlockThread _thread;
    //declare an array of the block sprites 
    private ArrayList<GraphicObject> _graphics = new ArrayList<GraphicObject>();


    public Blocks(Context context) {
        super(context);
        getHolder().addCallback(this);
        _thread = new BlockThread(getHolder(), this);
        setFocusable(true);
    }



    public void DrawBlocks(){
        for (int i=0; i < 20; i++)
        {
            //create new random x and y position values
            Random randX = new Random();
            int i1=randX.nextInt(getWidth()-0) + 0;
            Random randY = new Random();
            int i2=randY.nextInt(getHeight()-0) + 0;
            GraphicObject graphic = new GraphicObject(BitmapFactory.decodeResource(getResources(), R.drawable.block));
            graphic.getCoordinates().setX((int) i1 - graphic.getGraphic().getWidth() / 2);
            graphic.getCoordinates().setY((int) i2 - graphic.getGraphic().getWidth() / 2);
            _graphics.add(graphic);
        }
    }

    public void updatePhysics() {
        GraphicObject.Coordinates coord;
        for (GraphicObject graphic : _graphics) {
            //move blocks down
            coord = graphic.getCoordinates();
            coord.setY(coord.getY() + 5 );                
            // reset block
            if (coord.getY() + graphic.getGraphic().getHeight() > getHeight()+10) {
                coord.setY(-10);
            }
        }
    }

    @Override
    public void onDraw(Canvas canvas) {
        canvas.drawColor(Color.BLACK);
        Bitmap bitmap;
        GraphicObject.Coordinates coords;
        for (GraphicObject graphic : _graphics) {
            bitmap = graphic.getGraphic();
            coords = graphic.getCoordinates();
            canvas.drawBitmap(bitmap, coords.getX(), coords.getY(), null);
        }
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
        // TODO Auto-generated method stub
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        _thread.setRunning(true);
        _thread.start();
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        // simply copied from sample application LunarLander:
        // we have to tell thread to shut down & wait for it to finish, or else
        // it might touch the Surface after we return and explode
        boolean retry = true;
        _thread.setRunning(false);
        while (retry) {
            try {
                _thread.join();
                retry = false;
            } catch (InterruptedException e) {
                // we will try it again and again...
            }
        }
    }
}

class BlockThread extends Thread {
    private SurfaceHolder _surfaceHolder;
    private Blocks _Blocks;
    private boolean _run = false;

    public BlockThread(SurfaceHolder surfaceHolder, Blocks blocks) {
        _surfaceHolder = surfaceHolder;
        _Blocks = blocks;
    }

    public void setRunning(boolean run) {
        _run = run;
    }

    public SurfaceHolder getSurfaceHolder() {
        return _surfaceHolder;
    }

    @Override
    public void run() {
        Canvas c;
        _Blocks.DrawBlocks();

        while (_run) {
            c = null;
            try {
                c = _surfaceHolder.lockCanvas(null);
                synchronized (_surfaceHolder) {
                    _Blocks.updatePhysics();
                    _Blocks.onDraw(c);
                }
            } finally {
                // do this in a finally so that if an exception is thrown
                // during the above, we don't leave the Surface in an
                // inconsistent state
                if (c != null) {
                    _surfaceHolder.unlockCanvasAndPost(c);
                }
            }
        }
    }
}

class GraphicObject {
    //
     // Contains the coordinates of the graphic.
     //
    public class Coordinates {
        private int _x = 100;
        private int _y = 0;

        public int getX() {
            return _x + _bitmap.getWidth() / 2;
        }

        public void setX(int value) {
            _x = value - _bitmap.getWidth() / 2;
        }

        public int getY() {
            return _y + _bitmap.getHeight() / 2;
        }

        public void setY(int value) {
            _y = value - _bitmap.getHeight() / 2;
        }

        public String toString() {
            return "Coordinates: (" + _x + "/" + _y + ")";
        }
    }

    private Bitmap _bitmap;
    private Coordinates _coordinates;


    public GraphicObject(Bitmap bitmap) {
        _bitmap = bitmap;
        _coordinates = new Coordinates();

    }

    public Bitmap getGraphic() {
        return _bitmap;
    }



    public Coordinates getCoordinates() {
        return _coordinates;
    }
}
  • 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-14T08:15:57+00:00Added an answer on June 14, 2026 at 8:15 am

    The emulator is terribly slow and probably the cause of your issues. Nothing jumps out of your code, so try it on a real device before you tear your hair out trying to make it smoother.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I know there's a lot of other questions out there that deal with this
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,

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.