Incorrect clip rect in onDraw()?

by Leisuresuit Larry » Fri, 10 Apr 2009 01:40:34 GMT

Sponsored Links
 Hi all,

We came across a strange behavior. The clip rect of our custom view is
getting reset to its entire visible region, if we change the value of
another view.

Below is a sample app to demonstrate the behavior:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android=" 

    android:layout_height="100px" />

    android:layout_height="wrap_content" />

package com.test;

import android.os.Bundle;
import android.view.KeyEvent;
import android.widget.TextView;

public class ClipTest extends Activity {
    /** Called when the activity is first created. */
    public void onCreate(Bundle savedInstanceState) {

    public boolean onKeyDown(int keyCode, KeyEvent event) {
        TextView tv = (TextView)findViewById(;

        findViewById(, 0, 150, 75);
        return true;
package com.test;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;

public class MyView extends View {

    public MyView(Context context) {

    public MyView(Context context, AttributeSet attrs) {
        super(context, attrs);

    protected void onDraw(Canvas canvas) {

        Rect clip = canvas.getClipBounds();
        System.out.println("clip " + clip);

If you run the above app, MyView.onDraw() prints "clip Rect(0, 0, 320,
100)". If you comment out "tv.setText("Test");", then MyView.onDraw()
prints "clip Rect(100, 0, 150, 75)".

Why does setting the TextView's text affect MyView's clip rect? How
can we get the correct clip rect in MyView.onDraw()? Thanks for any



Incorrect clip rect in onDraw()?

by Romain Guy » Fri, 10 Apr 2009 02:46:52 GMT

 etText() invalidates the TextView entirely and when two invalidates
happen in the same UI event (as it is the case in your code), the
invalidate region becomes the union of the two original invalidate

On Wed, Apr 8, 2009 at 6:40 PM, Leisuresuit Larry <> wrote:

Romain Guy
Android framework engineer

Note: please don't send private questions to me, as I don't have time
to provide private support. All such questions should be posted on
public forums, where I and others can see and answer them


Sponsored Links

Incorrect clip rect in onDraw()?

by Leisuresuit Larry » Tue, 14 Apr 2009 02:30:56 GMT

 hanks for the explanation. We worked around this behavior send
separate messages to a Handler on the UI thread. I'm curious, though,
to why Android doesn't handle the invalidate regions separately.

On Apr 9, 11:46am, Romain Guy <> wrote:

Incorrect clip rect in onDraw()?

by Dianne Hackborn » Tue, 14 Apr 2009 02:45:53 GMT

 e very deliberately coalesce multiple invalidates into a single draw for
performance reasons (so we can perform a draw of the hierarchy once instead
of twice). This also avoids showing the UI in partial states, where one
part of the window has been updated to a new state but another part hasn't.
You probably shouldn't be working around this.

On Mon, Apr 13, 2009 at 11:30 AM, Leisuresuit Larry

Dianne Hackborn
Android framework engineer

Note: please don't send private questions to me, as I don't have time to
provide private support, and so won't reply to such e-mails. All such
questions should be posted on public forums, where I and others can see and
answer them.


Other Threads

1. what is the detailed relation between contacts and raw_contacts?

Any one can help interpret the relation between the table "contacts" and
"raw_contacts"?Is it one-to-one or one-to-many relation?I am puzzled because
the SDK seems indicate it is one-to-many relation but after I checkout all
data from the database table,I found it is one-to-one relation.

And is there any detailed relation diagram describing  android database



2. Error: "Upload a valid APK."

I recently joined the Android Market as a developer and whenever I try
to upload my .apk I only ever get this message: "Upload a valid APK."
I don't know what's wrong with it, I used the export wizard from
Eclipse. Any help is appreciated.

On a side note, I'm currently seeing "Your Registration to the Android
Market is still being processed. You can upload applications to the
Android Market but you cannot publish until your registration is
completed.", can someone tell me how long that'll last?

Thanks in advance.


3. DateFormat of android for AM/PM is not working properly with android 2.0

4. 3D (game) engines for Android

5. about working with kSoap

6. Calling web

7. Launch built-in Messaging Activity