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