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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T18:24:23+00:00 2026-05-17T18:24:23+00:00

I have created a simple test game that randomly spawns icons that move around

  • 0

I have created a simple test game that randomly spawns icons that move around the screen. When you click on an icon, it disappears. I used sample code from SDK, and various internet sites.

I want to change the background, it is being set to BLACK. I would like to use a picture I have.

 public void onDraw(Canvas canvas) {
       canvas.drawColor(Color.BLACK);
...
}

The background color is set on the onDraw. If I try to draw a bitmap instead, the game will eventually crash from a memory problem. (it runs out of memory). I believe this is because the image is being drawn every time onDraw is called.

I have tried placing it in various locations, but maybe I’m just missing something. Any help would be great. Below is the code I am using to create the background. My ideal goal would be to only create the background once at the start. And be able to update it when called for.

 Bitmap Background = BitmapFactory.decodeResource(getResources(),R.drawable.background);
 DisplayMetrics dm = new DisplayMetrics();
 getWindowManager().getDefaultDisplay().getMetrics(dm);
 int height = dm.heightPixels;
 int width = dm.widthPixels;
 Rect rec = new Rect(0, 0, width, height);
 canvas.drawBitmap(Background, null, rec, null);
  • 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-17T18:24:24+00:00Added an answer on May 17, 2026 at 6:24 pm

    Each time you call BitmapFactory.decodeResource() I believe you get a new Bitmap object. So this means a new byte[]. Each time your draw is called another byte array is allocated. It is important to call recycle() on a Bitmap after you are done with it to signal that this byte[] can be dealloc’d. So in your current implementation after the draw just call, Background.recycle();

    It is not an immediate dealloc so you may run into some performance issues. So you might also want to keep the single byte array in memory. Without more detail into where in the application this is happening it’s hard to give advice on how to do this. However if this code is in the draw() method for a particular component, just create the Bitmap in the constructor of the component and use it in the draw() method. Then before you forget about the component make sure to call recycle on the bitmap instance.

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

Sidebar

Related Questions

I have created a simple test form with FormBorderStyle = FixedToolWindow by default and
I have created a simple test harness in python for my ASP .net web
I have created a simple wcf service which used the WCF Service Library template.
I have created a simple grid of divs by left floating them and an
I have created a simple Asp.Net custom control which automatically combines all the correct
I have a simple GtkStatusBar created with glade,and want to add a simple message
I have a simple PowerShell script that uses WMI to create a web site
i have very simple problem. I need to create model, that represent element of
I recently created a turn-based game server that can accept 10s of thousands of
I've been trying to make a little simple game just to test my logics,

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.