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:

main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android=" http://schemas.android.com/apk/res/ 
android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >

<com.test.MyView
    android:id="@+id/view"
    android:layout_width="fill_parent"
    android:layout_height="100px" />

<TextView
    android:id="@+id/text"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" />

</LinearLayout>

ClipTest.java:
package com.test;

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

public class ClipTest extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        TextView tv = (TextView)findViewById(R.id.text);
        tv.setText("Test");

        findViewById(R.id.view).invalidate(100, 0, 150, 75);
        return true;
    }
}

MyView.java:
package com.test;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.View;

public class MyView extends View {

    public MyView(Context context) {
        super(context);
    }

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

    protected void onDraw(Canvas canvas) {
        super.onDraw(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
help!

Larry

--~--~---------~--~----~------------~-------~--~----~



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
regions.

On Wed, Apr 8, 2009 at 6:40 PM, Leisuresuit Larry <larrywan...@gmail.com> wrote:



--
Romain Guy
Android framework engineer
romain...@android.com

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 <romain...@google.com> 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
<larrywan...@gmail.com>wrote:



--
Dianne Hackborn
Android framework engineer
hack...@android.com

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. OOT + WTI : Paket Internet Terbaru dari SmartFren

ini smartfren tuh sama dengan Paket Smart biasa yah Bos ?

Thx

2011/3/18 Iwan <i...@robotijo.org>




-- 
Rgds,

Petra Immanuel

-- 
===============

2. Problem with SoundBoard App

I'm having a really strange problem:

I'm making a simple app with a lot of ImageButtons that make sounds
when you click it.

I've done that before, so I'm just copying a previous code for that
and replacing the names and ids.
But, after the 7th Button in the java code, the next ones "stop
unexpectedly" in the emulator when I click them.
start to have problems.

Apparently is not a code's problem, because when I just change the
position between them, the ones that were not working start working
and the ones that were normal, now stop working.

I'm really trying to solve this, but I don't have any clue. I'm just
copying the codes that are working right! And they become
functionless.

Here's the code: (note that the code is the same for all of them and
only the last one, "eurotrip" is not working. The others are fine. If
I just change the position eurotrip will work, but the other not.)

package rosalan.cinema.sounds;

import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageButton;

public class Menuprovisorio extends Activity {

        MediaPlayer ancora;
        MediaPlayer aneis;
        MediaPlayer jamesbond;
        MediaPlayer borat;
        MediaPlayer coringa;
        MediaPlayer devolta;
        MediaPlayer et;
        MediaPlayer eurotrip;
        MediaPlayer forrest;
        MediaPlayer godf;
        MediaPlayer hangover;
        MediaPlayer hg;
        MediaPlayer iluminado;
        MediaPlayer kill;
        MediaPlayer laranja;
        MediaPlayer old;
        MediaPlayer poderoso;
        MediaPlayer psico;
        MediaPlayer pulp;
        MediaPlayer scarface;
        MediaPlayer sparta;
        MediaPlayer spider;
        MediaPlayer starw;
        MediaPlayer terminator;
        MediaPlayer toy;
        MediaPlayer v;
        MediaPlayer vaderoutro;

        ImageButton a1;
        ImageButton a2;
        ImageButton a3;
        ImageButton a4;
        ImageButton a5;
        ImageButton a6;

        ImageButton a8;
        ImageButton a9;
        ImageButton b1;
        ImageButton b2;
        ImageButton b3;
        ImageButton b4;
        ImageButton b5;
        ImageButton b6;
        ImageButton b7;
        ImageButton b8;
        ImageButton b9;
        ImageButton c1;
        ImageButton c2;
        ImageButton c3;
        ImageButton c4;
        ImageButton c5;
        ImageButton c6;
        ImageButton c7;
        ImageButton c8;
        ImageButton c9;
        ImageButton a7;


        @Override
        protected void onCreate(Bundle savedInstanceState) {
                // TODO Auto-generated method stub
                super.onCreate(savedInstanceState);
                requestWindowFeature(Window.FEATURE_NO_TITLE);
                getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
                setContentView(R.layout.main);




                vaderoutro = MediaPlayer.create(this, R.raw.breath);

                 a8 = (ImageButton) findViewById(R.id.a8);
                 a8.setOnClickListener(new View.OnClickListener() {

                                @Override
                                public void onClick(View v) {
                                        // TODO Auto-generated method stub

                                        vaderoutro.start();
                                }
                        });
                 pulp = MediaPlayer.create(this, R.raw.pulp);

                 a9 = (ImageButton) findViewById(R.id.a9);
                 a9.setOnClickListener(new View.OnClickListener() {

                                @Override
                                public void onClick(View v) {
                                        // TODO Auto-generated method stub

                                        pulp.start();
                                }
                        });

                        jamesbond = MediaPlayer.create(this, R.raw.bond);

                        a1 = (ImageButton) findViewById(R.id.a1);
                        a1.setOnClickListener(new View.OnClickListener() {

                                @Override
                                public void onClick(View v) {
                                        // TODO Auto-generated method stub

                                        jamesbond.start();
                                }
                        });

                        scarface = MediaPlayer.create(this, R.raw.scarface);

                        a2 = (ImageButton) findViewById(R.id.a2);
                        a2.setOnClickListener(new View.OnClickListener() {

                                @Override
                                public void onClick(View v) {
                                        // TODO Auto-generated method stub

                                        scarface.start();
                                }
                        });
                 kill = MediaPlayer.create(this, R.raw.kill);

                 a4 = (ImageButton) findViewById(R.id.a4);
                 a4.setOnClickListener(new View.OnClickListener() {

                                @Override
                                public void onClick(View v) {
                                        // TODO Auto-generated method stub

                                        kill.start();
                                }
                        });

                        aneis = MediaPlayer.create(this, R.raw.aneis);

                        a5 = (ImageButton) findViewById(R.id.a5);
                        a5.setOnClickListener(new View.OnClickListener() {

                                @Override
                                public void onClick(View v) {
                                        // TODO Auto-generated method stub

                                        aneis.start();
                                }
                        });

                        coringa = MediaPlayer.create(this, R.raw.coringa);

                        a6 = (ImageButton) findViewById(R.id.a6);
                        a6.setOnClickListener(new View.OnClickListener() {

                                @Override
                                public void onClick(View v) {
                                        // TODO Auto-generated method stub

                                        coringa.start();
                                }
                        });

                        eurotrip = MediaPlayer.create(this, R.raw.eurotrip);

                        a7 = (ImageButton) findViewById(R.id.a7);
                        a7.setOnClickListener(new View.OnClickListener() {

                                @Override
                                public void onClick(View v) {

                                        eurotrip.start();
                                }
                        });


-- 

3. tanya koneksi Android ke BBM

4. Desire HD bisa flashdisk dan usb keyboard nih:-)

5. Any problems with setting screen time-out to 'Never'?

6. onItemLongClick and onItemClick for a gridview

7. Problem positioning scaled images