LoginSignup
0
1

More than 5 years have passed since last update.

SurfaceViewを使用するために3

Last updated at Posted at 2016-12-04

はじめに

SurfaceViewを使用して部分的な描画行ったときに
おかしな描画結果となりました。
そのときの失敗についてまとめました。

事例

SurfaceViewで部分的な再描画を行うとき
どの範囲を再描画するかをRectに設定してlockCanvasに渡します。
lockCanvasで渡したRectの設定値はlockCanvasの中で、
値が書き換えられているようです。

[誤]

MainSurfaceView.java
Rect rect = new Rect(0, 0, 100, 100);

Canvas canvas = this.surfaceView.getHolder.lockCanvas(rect);

if (null != canvas) {

    // lockCanvas内でrectの値が変更されているので期待した結果とならない
    canvas.drawBitmap(bitmap, src, rect, null);

    this.surfaceView.getHolder.unlockCanvas(canvas);
}

[正]

MainSurfaceView.java
Rect rect = new Rect(0, 0, 100, 100);

Canvas canvas = this.surfaceView.getHolder.lockCanvas(rect);

if (null != canvas) {

    // 再描画したい範囲の指定はlockCanvasで使用したRectではなく別のRectで行う
    Rect dst = new Rect(0, 0, 100, 100);

    canvas.drawBitmap(bitmap, src, dst, null);

    this.surfaceView.getHolder.unlockCanvas(canvas);
}

まとめ

参照渡しで引数をやり取りしているのでどこかのメソッドで使用された
オブジェクトを無条件で使用するときは注意が必要でした。

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1