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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T14:12:58+00:00 2026-05-23T14:12:58+00:00

I’m writing a game for Android and I’m having noticeable pauses every few seconds

  • 0

I’m writing a game for Android and I’m having noticeable pauses every few seconds when the GC runs. I’ve reduced the allocations to a minimum and the only remaining allocations (according to Allocation Tracker) are produced by Bitmap.createBitmap(..), an onTouchEvent and an onKeyEvent. Bitmap.createBitmap(..) is called twice per Frame, and cannot be removed.

Now to my question: Is there an alternative way to read the events and another way to create bitmaps? Or a way to pre-allocate the memory needed for the operations and a way of telling them to use that?

InputHandler class (contains listeners):

public class InputHandler {

private ISwarmInput mGame;

// Declare input types
public static final int USE_TOUCH = 0;
public static final int USE_MOTION = 1;
public static final int USE_KEY = 2;
public static final int USE_TOUCH_AND_KEY = 3;

private int mInputType;
public OrientationEventListener mOrientationListener;
public OnTouchListener mTouchListener;
public OnKeyListener mKeyListener;

public InputHandler(Context context, ISwarmInput game) {
    mGame = game;
    mTouchListener = createTouchListener();
    mKeyListener = createKeyListener();
}

public OrientationEventListener createTiltListener(Context context) {
    return new OrientationEventListener(context) {
        @Override
        public void onOrientationChanged(int orientation) {
            if (mInputType == USE_MOTION) {
                mGame.setAngle(orientation);
            }
        }
    };
}

public OnTouchListener createTouchListener() {
    return new OnTouchListener() {
        public boolean onTouch(View v, MotionEvent event){
            if (mInputType == USE_TOUCH || mInputType == USE_TOUCH_AND_KEY) {
                if (readPointIn(event.getX(), event.getY())) {
                    mGame.addNewPoint((int) event.getX(),
                            (int) event.getY());
                }
            }
            return true;
        }
    };
}

public OnKeyListener createKeyListener() {
    return new OnKeyListener() {
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            if (mInputType == USE_KEY || mInputType == USE_TOUCH_AND_KEY) {
                switch (keyCode) {
                case KeyEvent.KEYCODE_DPAD_UP:
                    mGame.setAngle(-90);
                    break;
                case KeyEvent.KEYCODE_DPAD_LEFT:
                    mGame.setAngle(180);
                    break;
                case KeyEvent.KEYCODE_DPAD_DOWN:
                    mGame.setAngle(90);
                    break;
                case KeyEvent.KEYCODE_DPAD_RIGHT:
                    mGame.setAngle(0);
                    break;
                }
            }
            return false;
        }
    };
}
}

Bitmap-creating part

public void drawHead(Canvas canv) {
    mat.reset();
    nextFrame();

    setRotationAndFlip(mat);

    this.bmp = Bitmap.createBitmap(SPRITESHEET, Bird.mCurFrame * BIG_W[mUseBird], 0,
            BIG_W[mUseBird], BIG_H[mUseBird], mat, true);
    super.drawPlaceable(canv);

    mat.preScale((float)0.6, (float)0.6);
    this.bmp = Bitmap.createBitmap(SPRITESHEET, Bird.mCurFrame * BIG_W[mUseBird], 0,
            BIG_W[mUseBird], BIG_H[mUseBird], mat, true);
}

The game is something similar to snake, you’re a bird and for each worm you eat you get a smaller bird fallowing you. The second bitmap is for the following, smaller bird.

  • 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-23T14:12:58+00:00Added an answer on May 23, 2026 at 2:12 pm

    For reusable input events you might want to take a look at this implementation: http://code.google.com/p/libgdx/source/browse/trunk/backends/gdx-backend-android/src/com/badlogic/gdx/backends/android/AndroidInput.java

    Regarding bitmaps… maybe you should make some sprites the same size so you can reuse your bitmap.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I want use html5's new tag to play a wav file (currently only supported
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I am writing an app with both english and french support. The app requests
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but

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.