绘图 Painter转接口封装的方式
2013-11-21 11:52 Clingingboy 阅读(362) 评论(0) 编辑 收藏 举报
记录下思想
适用于业务逻辑相对单纯的一些画法,比如画背景(颜色,背景,边框等)
一个Draw方法中如果绘制比较复杂的话,就会导致代码混乱,而不灵活,每次需求更改就得重新画过,可重用性差.
以接口的方式可以很好的重复利用功能,不必因为需求的更改而大量变更代码
chrome的方法定义如下
// Painting ------------------------------------------------------------------
// Background
scoped_ptr<Background> background_;
// Border.
scoped_ptr<Border> border_;
// Focus border.
scoped_ptr<FocusBorder> focus_border_;
// The background object is owned by this object and may be NULL.
void set_background(Background* b) { background_.reset(b); }
const Background* background() const { return background_.get(); }
Background* background() { return background_.get(); }
// The border object is owned by this object and may be NULL.
void set_border(Border* b) { border_.reset(b); }
const Border* border() const { return border_.get(); }
Border* border() { return border_.get(); }
// The focus_border object is owned by this object and may be NULL.
void set_focus_border(FocusBorder* b) { focus_border_.reset(b); }
const FocusBorder* focus_border() const { return focus_border_.get(); }
FocusBorder* focus_border() { return focus_border_.get(); }
具体调用
void View::OnPaintBackground(gfx::Canvas* canvas) {
if (background_.get()) {
TRACE_EVENT2("views", "View::OnPaintBackground",
"width", canvas->sk_canvas()->getDevice()->width(),
"height", canvas->sk_canvas()->getDevice()->height());
background_->Paint(canvas, this);
}
}
void View::OnPaintBorder(gfx::Canvas* canvas) {
if (border_.get()) {
TRACE_EVENT2("views", "View::OnPaintBorder",
"width", canvas->sk_canvas()->getDevice()->width(),
"height", canvas->sk_canvas()->getDevice()->height());
border_->Paint(*this, canvas);
}
}
void View::OnPaintFocusBorder(gfx::Canvas* canvas) {
if (focus_border_.get() &&
HasFocus() && (focusable() || IsAccessibilityFocusable())) {
TRACE_EVENT2("views", "views::OnPaintFocusBorder",
"width", canvas->sk_canvas()->getDevice()->width(),
"height", canvas->sk_canvas()->getDevice()->height());
focus_border_->Paint(*this, canvas);
}
}