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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T14:15:30+00:00 2026-06-01T14:15:30+00:00

In my custom view I’m looking into using Canvas.getClipBounds() to optimize my onDraw method

  • 0

In my custom view I’m looking into using Canvas.getClipBounds() to optimize my onDraw method (so that I’m only drawing what’s absolutely necessary each time it’s called).

However, I still want to absolutely avoid any object creation…

My question, therefore is: does getClipBounds() allocate a new Rect each time it is called? Or is it simply recycling a single Rect?

And if it is allocating a new object, can I save this expense by using getClipBounds(Rect bounds), which seems to use the passed Rect instead of its own?


(before you scream premature optimization, do consider that when placed in a ScrollView, onDraw can be called many times each second)

  • 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-01T14:15:31+00:00Added an answer on June 1, 2026 at 2:15 pm

    I have looked into the source code for Canvas as of Android 4.0.1, and it is as follows:

    /**
     * Retrieve the clip bounds, returning true if they are non-empty.
     *
     * @param bounds Return the clip bounds here. If it is null, ignore it but
     *               still return true if the current clip is non-empty.
     * @return true if the current clip is non-empty.
     */
    public boolean getClipBounds(Rect bounds) {
        return native_getClipBounds(mNativeCanvas, bounds);
    }
    
    
    /**
     * Retrieve the clip bounds.
     *
     * @return the clip bounds, or [0, 0, 0, 0] if the clip is empty.
     */
    public final Rect getClipBounds() {
        Rect r = new Rect();
        getClipBounds(r);
        return r;
    }
    

    So, answering your question, getClipBounds(Rect bounds) will spare you from one object creation, but getClipBounds() will actually create a new Rect() object every time it is called.

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

Sidebar

Related Questions

I currently have a custom view whose color is set using the following, canvas.drawColor(Color.RED);
I have a custom view which I override the onDraw method to draw bitmaps
I have a simple custom view that is connected via outlet to a NIB.
I have a custom view that accepts drags. In the custom view there is
I created a custom view that uses Json.Simple to serialize the model and write
I have implemented a custom view within another activity into a XML layout. The
A problem with a custom view adapter that implement listeners form button and chronometer.
I have a custom view in my Android App that uses a ScaleGestureDetector.SimpleOnScaleGestureListener to
HI have a custom view class that is loaded and placed within my main
I created a custom View by extending from View. In onDraw() I managed to

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.