public class OverlayView extends View {
private Rect rectangle;
private Paint paint;
int l,t,r,b;
private final List<DrawCallback> callbacks = new LinkedList<DrawCallback>();
public OverlayView(final Context context, final AttributeSet attrs) {
super(context, attrs);
}
public void addCallback(final DrawCallback callback) {
callbacks.add(callback);
}
@Override
public void onDraw(Canvas canvas) {
Log.v("drawrectindraw",String.valueOf(l)+" "+String.valueOf(t)+" "+String.valueOf(r)+" "+String.valueOf(b));
rectangle = new Rect(l, t, r, b);
paint = new Paint();
paint.setColor(Color.WHITE);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(5);
canvas.drawRect(rectangle, paint);
}
public OverlayView(Context context, int l, int t, int r, int b) {
super(context);
this.l = l;
this.t = t;
this.r = r;
this.b = b;
rectangle = new Rect(this.l, this.t, this.r, this.b);
Log.v("drawrect",String.valueOf(this.l)+" "+String.valueOf(t)+" "+String.valueOf(r)+" "+String.valueOf(b));
}
public interface DrawCallback {
public void drawCallback(final Canvas canvas);
}
}
画面に四角形を継続的に描画するために、アクティビティからカスタム ビュー クラスに四角形の値を渡しています。
onDraw メソッドでなぜ 0 0 0 0 なのか理解できません。コンストラクターで適切な値を取得できます。
例: drawrect: 153 494 535 875
ログ
02-25 18:02:28.795 30639-30639/V/drawrectindraw: 0 0 0 0
02-25 18:02:29.025 30639-31224/V/drawrect: 153 494 535 875
02-25 18:02:29.164 30639-30639/V/drawrectindraw: 0 0 0 0
02-25 18:02:29.366 30639-31412/V/drawrect: 153 494 535 875
02-25 18:02:29.516 30639-30639/V/drawrectindraw: 0 0 0 0
02-25 18:02:29.745 30639-30767/V/drawrect: 199 481 517 801
02-25 18:02:29.919 30639-30639/V/drawrectindraw: 0 0 0 0