グラフィックス(10)-その他のCanvasクラスのメソッド


CanvasのMatrixとclipの状態の保存と復元

Canvasの、Matrixとclipの状態の保存と復元を、おこなう事ができます。

Matrixについては、「グラフィックス(8)-Canvasの移動、拡大・縮小、回転、歪(傾き)とMatrixの操作」を、参照して下さい。

clipについては、「グラフィックス(9)-描画領域のクリップ」を、参照して下さい。

保存と復元に関する、Canvasクラスのメソッドを、「Android Developers」のCanvasクラスのリファレンスより、抜粋して以下に列挙します。

  • int save()
    Saves the current matrix and clip onto a private stack.
  • int save(int saveFlags)
    Based on saveFlags, can save the current matrix and clip onto a private stack.
  • void restore()
    This call balances a previous call to save(), and is used to remove all modifications to the matrix/clip state since the last save call.
  • void restoreToCount(int saveCount)
    Efficient way to pop any calls to save() that happened after the save count reached saveCount.
  • int getSaveCount()
    Returns the number of matrix/clip states on the Canvas' private stack.

以下に、saveメソッドとrestoreメソッドのプログラムの例を、示します。

このプログラムを実行すると、以下のような画面が表示されます。

画面の、上下左右の端のがクリップされて、座標 (50 , 50)の点からBitmapが描画され、更に、この点を起点に45度回転して、Bitmapが描画されています。

 

 

このプログラムの、3行目と8行目のコメントを削除して、saveメソッドとrestoreメソッドを有効にして、プログラムを実行すると、以下のような画面が表示されます。

 

 

6行目の、drawBitmapで描画されたBitmapは端のがクリップされて、座標 (50 , 50)の点から描画されているのに対して、

10行目のdrawBitmapで描画されたBitmapは端のがクリップされずに、座標 (0 , 0)の点を起点に45度回転して、描画されているのがわかると思います。

saveメソッドで保存されたMatrixとclipのデータは、スタックに保存されていて、何段階か前の状態に戻すこともできるようです。

Pictureの描画

Pictureオブジェクトの描画に関する、Canvasクラスのメソッドを、「Android Developers」のCanvasクラスのリファレンスより、抜粋して以下に列挙します。

  • void drawPicture(Picture picture, RectF dst)
    Draw the picture, stretched to fit into the dst rectangle.
  • void drawPicture(Picture picture, Rect dst)
    Draw the picture, stretched to fit into the dst rectangle.
  • void drawPicture(Picture picture)
    Save the canvas state, draw the picture, and restore the canvas state.

Pictureオブジェクトが、どのようなものかは、よくわかりませんでした。

Pictureオブジェクトの例については、ApiDemosの Pictures.java Android Developersが、参考になりそうです。

その他のCanvasクラスのメソッド

いままでの記事で触れていないCanvasクラスのメソッドを、 「Android Developers」のCanvasクラスのリファレンスより、抜粋して、以下に列挙しておきます。

  • static void freeGlCaches()
    Call this to free up OpenGL resources that may be cached or allocated on behalf of the Canvas.
  • int getDensity()
    Returns the target density of the canvas.
  • DrawFilter getDrawFilter()
  • GL getGL()
    Return the GL object associated with this canvas, or null if it is not backed by GL.
  • boolean isOpaque()
    Return true if the device that the current layer draws into is opaque (i.e.
  • boolean quickReject(Path path, Canvas.EdgeType type)
    Return true if the specified path, after being transformed by the current matrix, would lie completely outside of the current clip.
  • boolean quickReject(float left, float top, float right, float bottom, Canvas.EdgeType type)
    Return true if the specified rectangle, after being transformed by the current matrix, would lie completely outside of the current clip.
  • boolean quickReject(RectF rect, Canvas.EdgeType type)
    Return true if the specified rectangle, after being transformed by the current matrix, would lie completely outside of the current clip.
  • final void rotate(float degrees, float px, float py)
    Preconcat the current matrix with the specified rotation.
  • int saveLayer(float left, float top, float right, float bottom, Paint paint, int saveFlags)
    Helper version of saveLayer() that takes 4 values rather than a RectF.
  • int saveLayer(RectF bounds, Paint paint, int saveFlags)
    This behaves the same as save(), but in addition it allocates an offscreen bitmap.
  • int saveLayerAlpha(float left, float top, float right, float bottom, int alpha, int saveFlags)
    Helper for saveLayerAlpha() that takes 4 values instead of a RectF.
  • int saveLayerAlpha(RectF bounds, int alpha, int saveFlags)
    This behaves the same as save(), but in addition it allocates an offscreen bitmap.
  • void setDensity(int density)
    Specifies the density for this Canvas' backing bitmap.
  • void setDrawFilter(DrawFilter filter)
  • void setViewport(int width, int height)
    Set the viewport dimensions if this canvas is GL based.

 

ページのトップへ戻る