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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T04:03:40+00:00 2026-06-12T04:03:40+00:00

it’s my first time asking a question here so I’ll try to stay on-topic.

  • 0

it’s my first time asking a question here so I’ll try to stay on-topic. I’m trying to randomly generate a background by creating an appropriately-sized ArrayList of Bitmap objects, and drawing them in order. This implementation works fine loading a single Bitmap, by the way; it’s just stumbling with a list.

Before I get to the code, I’d like to point out that Ideally I would make a single Bitmap by adding the individual pixels or tiles, and indeed have tried a few variations of that, but they all result in black screens; I’m starting to think it might be a problem with how I draw to the Canvas. Anyways, here’s what I have:

First, I generate the random ArrayList, only using 3 colors right now. I’d make it return the list, but it’s just a private method inside the thread referencing one of the thread’s variables so it doesn’t matter much.

    private void genMap(Resources res)
    {
        // Load basic tiles.
        Bitmap green = BitmapFactory.decodeResource(res, R.drawable.green);
        Bitmap red = BitmapFactory.decodeResource(res, R.drawable.red);
        Bitmap blue = BitmapFactory.decodeResource(res, R.drawable.blue);

                    // All tiles must be the same size.
        int tile_width = green.getWidth();
        int tile_height = green.getHeight();
        int num_x = mCanvasWidth / tile_width;
        int num_y = mCanvasHeight / tile_height;

        for (int j = 0; j < num_y; j++)
        {
            for (int i = 0; i < num_x; i++)
            {
                double r = Math.random();
                Bitmap tile;
                if (r <= 1/3) {tile = green;}
                else if (r <= 2/3) {tile = red;}
                else {tile = blue;}
                // Create a new Bitmap in order to avoid referencing the old value.
                mBackgroundImages.add(Bitmap.createBitmap(tile));
            }
        }
    }

So, that’s how the random values are mapped to a pattern. The method is called in the thread’s constructor, which is in turn called every time onCreate is called; for now, I’m just clearing the list and making a new random pattern each time:

...
Resources res = context.getResources();
mBackgroundImages = new ArrayList<Bitmap>();
genMap(res);
...

And finally, the draw method; it works fine loading a single Bitmap via BitmapFactory.decodeResources, but shows a black screen when doing this:

    private void doDraw(Canvas canvas)
    {
        /* Draw the bg.
         * Remember, Canvas objects accumulate.
         * So drawn first = most in the background. */
        if (canvas != null)
        {
            if (mBackgroundImages.size() > 0)
            {
                int tile_width = mBackgroundImages.get(0).getWidth();
                int tile_height = mBackgroundImages.get(0).getHeight();
                for (int y = 0; y < mCanvasHeight / tile_height; y++)
                {
                    for(int x = 0; x < mCanvasWidth / tile_width; x++)
                    {
                         // Draw the Bitmap at the correct position in the list; Y * XWIDTH + X, at pos X * XWIDTH, Y * YWIDTH.
                         canvas.drawBitmap(mBackgroundImages.get((x + y * (mCanvasWidth/tile_width))), x * tile_width, y * tile_height, null);
                    }
                }
            }
        }
    }

Any help would be appreciated, thanks.

  • 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-12T04:03:41+00:00Added an answer on June 12, 2026 at 4:03 am

    Have you double-checked the order of precedence of the following line?

    ((x + y * (mCanvasWidth/tile_width)))
    

    Interesting question though: are you drawing solid colors? I would also see if this is actually making a Bitmap:

    mBackgroundImages.add(Bitmap.createBitmap(tile));
    

    You might actually be able to just keep references instead of creating a lot of bitmaps, but that can come later

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

Sidebar

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to render a haml file in a javascript response like so:
We're building an app, our first using Rails 3, and we're having to build
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
This could be a duplicate question, but I have no idea what search terms
I'm trying to select an H1 element which is the second-child in its group

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.