To be honest, if you want something like the Swing behavior then don't use
SurfaceView.  The regular view hierarchy behaves more like Swing -- you call
invalidate() on the rectangle that has changed, and the rectangles will be
combined into a single draw operation down the view hierarchy.

The whole point of SurfaceView is to have something that you can directly
draw into whenever you want, with your full control.  If you want to batch
stuff like that, you will need to implement all of the batching yourself.

On Wed, May 25, 2011 at 6:55 PM, yccheok <[email protected]> wrote:

> I try to apply the similar technique found in Swing over Android's
> SurfaceView
>
> http://download.oracle.com/javase/tutorial/uiswing/painting/step3.html
>
> "An important point worth noting is that although we have invoked
> repaint twice in a row in the same event handler, Swing is smart
> enough to take that information and repaint those sections of the
> screen all in one single paint operation. In other words, Swing will
> not repaint the component twice in a row, even if that is what the
> code appears to be doing. "
>
> Here is the code. Swing only repaint area which involves object
> movement, but NOT entire client screen area.
>
>
> private void moveSquare(int x, int y) {
>        int OFFSET = 1;
>        if ((squareX!=x) || (squareY!=y)) {
>            repaint(squareX,squareY,squareW+OFFSET,squareH+OFFSET);
>            squareX=x;
>            squareY=y;
>            repaint(squareX,squareY,squareW+OFFSET,squareH+OFFSET);
>        }
> }
>
> I try to mimic the technique, by having the following code.
>
>
>
> private void moveSquare(int x, int y) {
>        int OFFSET = 1;
>        if ((squareX!=x) || (squareY!=y)) {
>
>        try {
>                c = mSurfaceHolder.lockCanvas(new
> Rect(squareX,squareY,squareX
> +squareW+OFFSET,squareY+squareH+OFFSET));
>                synchronized (mSurfaceHolder) {
>                        doDraw(c);
>                }
>        } finally {
>                if (c != null) {
>                        mSurfaceHolder.unlockCanvasAndPost(c);
>                }
>        }
>
>        squareX=x;
>        squareY=y;
>
>        try {
>                c = mSurfaceHolder.lockCanvas(new
> Rect(squareX,squareY,squareX
> +squareW+OFFSET,squareY+squareH+OFFSET));
>                synchronized (mSurfaceHolder) {
>                        doDraw(c);
>                }
>        } finally {
>                if (c != null) {
>                        mSurfaceHolder.unlockCanvasAndPost(c);
>                }
>        }
>        }
> }
>
> But it doesn't work. Does anyone know what is the correct technique,
> or, this technique cannot be applied on SurfaceView, but only
> conventional View by using postInvalidate?
>
> --
> You received this message because you are subscribed to the Google
> Groups "Android Developers" group.
> To post to this group, send email to [email protected]
> To unsubscribe from this group, send email to
> [email protected]
> For more options, visit this group at
> http://groups.google.com/group/android-developers?hl=en
>



-- 
Dianne Hackborn
Android framework engineer
[email protected]

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.

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to