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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T01:11:12+00:00 2026-06-08T01:11:12+00:00

I am attempting to draw a bitmap along a rectangle, but I am not

  • 0

I am attempting to draw a bitmap along a rectangle, but I am not sure how to go about it. Is there a way to tile a bitmap along a Rect object using a paint property or something? I have looked, but I can’t find anything that makes it do what I need it too, most of the tiling options won’t tile it for a specific instance, they tile it along the entire screen, so everything using that bitmap ends up having one big bitmap tiling along all of them at the same time, without scrolling or anything.

Any ideas? If you need more info let me know, its kind of a weird question so I know I probably didn’t mention something important.

William

  • 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-08T01:11:14+00:00Added an answer on June 8, 2026 at 1:11 am

    There are a couple ways that you can attack this. I’ll outline two of them here…

    One way:

    You can define a BitmapDrawable around the Bitmap. Set its TileMode to repeat. Give it some bounds via setBounds(rect) where rect is your Rect instance.

    Here’s a brief example using a View onDraw as context:

    public class MyView extends View {
        Rect rect;
        Bitmap mBitmap;
        BitmapDrawable mDrawable;
    
        public MyView(Context context) {
            super(context);
            rect = new Rect(0, 0, 100, 100);
            mBitmap = loadBitmap();
            mDrawable = new BitmapDrawable(context.getResources(), mBitmap);
            mDrawable.setTileModeXY(TileMode.REPEAT, TileMode.REPEAT);
            mDrawable.setBounds(rect);
        }
    
        @Override
        protected void onDraw(Canvas canvas) {
            super.onDraw(canvas);
            mDrawable.draw(canvas);
        }
    
        public Bitmap loadBitmap() {
            // included only for example sake
            Paint paint = new Paint();
            paint.setColor(Color.RED);
            Bitmap bm = Bitmap.createBitmap(10, 10, Bitmap.Config.ARGB_8888);
            Canvas canvas = new Canvas(bm);
            canvas.drawRect(0,0,10,10, paint);
            paint.setStyle(Style.STROKE);
            paint.setColor(Color.BLUE);
            canvas.drawRect(0, 0, 9, 9, paint);
            return bm;
        }
    }
    

    Note:

    You can also define a BitmapDrawable in xml instead of doing it in code.

    Also, if you know that a drawable you are loading via getResources().getDrawable(resourceId) is indeed just a Bitmap, you can cast it to a BitmapDrawable and set the TileMode there. A la:

    public class MyView extends View {
        Rect rect;
        BitmapDrawable mDrawable;
    
        public MyView(Context context) {
            super(context);
            rect = new Rect(0, 0, 400, 240);
            mDrawable = (BitmapDrawable) context.getResources().getDrawable(R.drawable.ic_launcher);
            mDrawable.setTileModeXY(TileMode.REPEAT, TileMode.REPEAT);
            mDrawable.setBounds(rect);
    
            this.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.ic_launcher));
        }
    
        @Override
        protected void onDraw(Canvas canvas) {
            super.onDraw(canvas);
            mDrawable.draw(canvas);
        }
    }
    

    This example shows the background being stretched, and a tiled version being draw over top of it.

    Another way:

    Loop in the x and y direction and repeatedly draw the bitmap into the rect:

    public class MyView extends View {
        Rect rect;
        Bitmap mBitmap;
    
        public MyView(Context context) {
            super(context);
            rect = new Rect(0, 0, 100, 100);
            mBitmap = loadBitmap();
        }
    
        @Override
        protected void onDraw(Canvas canvas) {
            super.onDraw(canvas);
            final int bmWidth = mBitmap.getWidth();
            final int bmHeight = mBitmap.getHeight();
    
            for (int y = 0, height = rect.height(); y < height; y += bmHeight) {
                for (int x = 0, width = rect.width(); x < width; x += bmWidth) {
                    canvas.drawBitmap(mBitmap, x, y, null);
                }
            }
        }
    
        public Bitmap loadBitmap() {
            // included only for example sake
            Paint paint = new Paint();
            paint.setColor(Color.RED);
            Bitmap bm = Bitmap.createBitmap(10, 10, Bitmap.Config.ARGB_8888);
            Canvas canvas = new Canvas(bm);
            canvas.drawRect(0,0,10,10, paint);
            paint.setStyle(Style.STROKE);
            paint.setColor(Color.BLUE);
            canvas.drawRect(0, 0, 9, 9, paint);
            return bm;
        }
    }
    

    I personally would go for the first (BitmapDrawable) method.

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

Sidebar

Related Questions

i'm attempting to draw a red stroke (1 thickness) around a rounded rectangle but
The only way I found so far is System.Drawing.Bitmap.GetPixel() , but Microsoft has warnings
I am attempting to draw a triangular portion of a bitmap. I already know
I'm attempting to draw text to the screen using GLUT in 2d. I want
I've posted this question in gis.stackexchange earlier but I'm not sure if that was
Using OpenGL I'm attempting to draw a primitive map of my campus. Can anyone
I'm attempting to draw a graph using Cubic Hermite Splines . I grabbed the
I am attempting to draw a slightly transparent blue rectangle in Native Win32 C++.
I am attempting to draw a string using Slick2D 's UnicodeFont . Unfortunately it
I am attempting to draw a custom Drawable overlay on a MapView. I use

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.