最近在看一个五子棋的游戏,每次下棋子都要重新画棋盘,觉得这很浪费资源,所以想请问下大家该如何处理,或者保存canvas的状态,避免每次都要画棋子。
现在有个方法invalidate(Rect dirty)同样也会调用onDraw()方法,但是该如何处理才能做到局部刷新呢

解决方案 »

  1.   

    public void invalidate (Rect dirty) 
    Since: API Level 1 Mark the the area defined by dirty as needing to be drawn. If the view is visible, onDraw(Canvas) will be called at some point in the future. This must be called from a UI thread. To call from a non-UI thread, call postInvalidate(). WARNING: This method is destructive to dirty.此函数只是标记rect区域为有效区(弄脏即需重画).只要调用invalidate()都会呼叫OnDraw的.只不过系统在内部双缓冲或映射至屏幕或显存时,只会映射rect区域,并不是onDraw里自动也只会在内存画布里只绘制这部份区域.如果你需要在画布中也只绘制rect的区域,可能你需要自己在OnDraw里去实现.只绘那一部份.
      

  2.   

    setWillNotDraw(),当你不需要的onDraw()的时候用
      

  3.   


    canvas.save();
    canvas.clipRect(left, top, right, bottom);//你要刷新的区域
    canvas.drawBitmap(bitmap, left, top, aPaint);
    canvas.restore();