1

我试过 Romain Guy 的 TextureView 示例代码 (http://pastebin.com/J4uDgrZ8),效果很好。但是当我将 lockCanvas(null) 更改为 lockCanvas(new Rect(x, y, x+20, y+20)) 时,示例开始闪烁。似乎'lockCanvas(Rect)'对于TextureView或任何其他原因不能很好地工作?

我正在使用带有 android 4.0.3 的摩托罗拉 XOOM。谢谢你的帮助!

我修改的代码如下:

 public void run() {
        float x = 0.0f;
        float y = 0.0f;
        float speedX = 5.0f;
        float speedY = 3.0f;

        Paint paint = new Paint();
        paint.setColor(0xff00ff00);

        while (mRunning && !Thread.interrupted()) {
            //final Canvas canvas = mSurface.lockCanvas(null);
            **final Canvas canvas = mSurface.lockCanvas(new Rect((int)x, (int)y,
                                                               (int)(x+20.0f), (int)(y+20.0f)));**  
            try {
                canvas.drawColor(0x00000000, PorterDuff.Mode.CLEAR);
                canvas.drawRect(x, y, x + 20.0f, y + 20.0f, paint);
            } finally {
                mSurface.unlockCanvasAndPost(canvas);
            }

            if (x + 20.0f + speedX >= mSurface.getWidth() || x + speedX <= 0.0f) {
                speedX = -speedX;
            }
            if (y + 20.0f + speedY >= mSurface.getHeight() || y + speedY <= 0.0f) {
                speedY = -speedY;
            }

            x += speedX;
            y += speedY;

            try {
                //Thread.sleep(15);
                  **Thread.sleep(1);**
            } catch (InterruptedException e) {
                // Interrupted
            }
        }

我仔细检查了一下,发现是“Thread.sleep(1)”加上“lockCanvas(Rect)”导致闪烁。使用 lockCanvas(null) 时, sleep(1) 是可以的。所以 lockCanvas(Rect) 不能像 lockCanvas(null) 一样快刷新??

4

0 回答 0