1 #include "SampleCode.h"
2 #include "SkCanvas.h"
3 #include "SkPaint.h"
4 #include "SkView.h"
5
6 ///////////////////////////////////////////////////////////////////////////////
7
8 class LayerMaskView : public SampleView {
9 public:
LayerMaskView()10 LayerMaskView() {
11 this->setBGColor(0xFFDDDDDD);
12 }
13
14 protected:
15 // overrides from SkEventSink
onQuery(SkEvent * evt)16 virtual bool onQuery(SkEvent* evt) {
17 if (SampleCode::TitleQ(*evt)) {
18 SampleCode::TitleR(evt, "LayerMask");
19 return true;
20 }
21 return this->INHERITED::onQuery(evt);
22 }
23
drawMask(SkCanvas * canvas,const SkRect & r)24 void drawMask(SkCanvas* canvas, const SkRect& r) {
25 SkPaint paint;
26 paint.setAntiAlias(true);
27
28 if (true) {
29 SkBitmap mask;
30 int w = SkScalarRound(r.width());
31 int h = SkScalarRound(r.height());
32 mask.setConfig(SkBitmap::kARGB_8888_Config, w, h);
33 mask.allocPixels();
34 mask.eraseColor(0);
35 SkCanvas c(mask);
36 SkRect bounds = r;
37 bounds.offset(-bounds.fLeft, -bounds.fTop);
38 c.drawOval(bounds, paint);
39
40 paint.setXfermodeMode(SkXfermode::kDstIn_Mode);
41 canvas->drawBitmap(mask, r.fLeft, r.fTop, &paint);
42 } else {
43 SkPath p;
44 p.addOval(r);
45 p.setFillType(SkPath::kInverseWinding_FillType);
46 paint.setXfermodeMode(SkXfermode::kDstOut_Mode);
47 canvas->drawPath(p, paint);
48 }
49 }
50
onDrawContent(SkCanvas * canvas)51 virtual void onDrawContent(SkCanvas* canvas) {
52 SkRect r;
53 r.set(SkIntToScalar(20), SkIntToScalar(20), SkIntToScalar(120), SkIntToScalar(120));
54 canvas->saveLayer(&r, NULL, SkCanvas::kARGB_ClipLayer_SaveFlag);
55 canvas->drawColor(SK_ColorRED);
56 drawMask(canvas, r);
57 canvas->restore();
58 }
59
60 private:
61 typedef SampleView INHERITED;
62 };
63
64 ///////////////////////////////////////////////////////////////////////////////
65
MyFactory()66 static SkView* MyFactory() { return new LayerMaskView; }
67 static SkViewRegister reg(MyFactory);
68
69