2011-07-28 14:26:00 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2011 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
2015-08-05 20:57:49 +00:00
|
|
|
|
2009-05-05 23:13:23 +00:00
|
|
|
#include "SampleCode.h"
|
2017-05-03 19:16:58 +00:00
|
|
|
#include "SkBitmap.h"
|
2009-05-05 23:13:23 +00:00
|
|
|
#include "SkCanvas.h"
|
|
|
|
#include "SkPaint.h"
|
2015-08-05 20:57:49 +00:00
|
|
|
#include "SkPath.h"
|
2009-05-05 23:13:23 +00:00
|
|
|
#include "SkView.h"
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2011-05-05 01:59:48 +00:00
|
|
|
class LayerMaskView : public SampleView {
|
2009-05-05 23:13:23 +00:00
|
|
|
public:
|
2012-08-23 18:19:56 +00:00
|
|
|
LayerMaskView() {
|
2011-05-05 01:59:48 +00:00
|
|
|
this->setBGColor(0xFFDDDDDD);
|
|
|
|
}
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2009-05-05 23:13:23 +00:00
|
|
|
protected:
|
|
|
|
// overrides from SkEventSink
|
|
|
|
virtual bool onQuery(SkEvent* evt) {
|
|
|
|
if (SampleCode::TitleQ(*evt)) {
|
|
|
|
SampleCode::TitleR(evt, "LayerMask");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return this->INHERITED::onQuery(evt);
|
|
|
|
}
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2009-05-05 23:13:23 +00:00
|
|
|
void drawMask(SkCanvas* canvas, const SkRect& r) {
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setAntiAlias(true);
|
|
|
|
|
|
|
|
if (true) {
|
|
|
|
SkBitmap mask;
|
2013-12-17 19:22:07 +00:00
|
|
|
int w = SkScalarRoundToInt(r.width());
|
|
|
|
int h = SkScalarRoundToInt(r.height());
|
2014-02-17 02:55:57 +00:00
|
|
|
mask.allocN32Pixels(w, h);
|
2012-12-06 21:47:40 +00:00
|
|
|
mask.eraseColor(SK_ColorTRANSPARENT);
|
2009-05-05 23:13:23 +00:00
|
|
|
SkCanvas c(mask);
|
|
|
|
SkRect bounds = r;
|
|
|
|
bounds.offset(-bounds.fLeft, -bounds.fTop);
|
|
|
|
c.drawOval(bounds, paint);
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2016-10-06 00:33:02 +00:00
|
|
|
paint.setBlendMode(SkBlendMode::kDstIn);
|
2009-05-05 23:13:23 +00:00
|
|
|
canvas->drawBitmap(mask, r.fLeft, r.fTop, &paint);
|
|
|
|
} else {
|
|
|
|
SkPath p;
|
|
|
|
p.addOval(r);
|
|
|
|
p.setFillType(SkPath::kInverseWinding_FillType);
|
2016-10-06 00:33:02 +00:00
|
|
|
paint.setBlendMode(SkBlendMode::kDstOut);
|
2009-05-05 23:13:23 +00:00
|
|
|
canvas->drawPath(p, paint);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-05-05 01:59:48 +00:00
|
|
|
virtual void onDrawContent(SkCanvas* canvas) {
|
2009-05-05 23:13:23 +00:00
|
|
|
SkRect r;
|
|
|
|
r.set(SkIntToScalar(20), SkIntToScalar(20), SkIntToScalar(120), SkIntToScalar(120));
|
2015-08-27 14:41:13 +00:00
|
|
|
canvas->saveLayer(&r, nullptr);
|
2009-05-05 23:13:23 +00:00
|
|
|
canvas->drawColor(SK_ColorRED);
|
|
|
|
drawMask(canvas, r);
|
|
|
|
canvas->restore();
|
|
|
|
}
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2009-05-05 23:13:23 +00:00
|
|
|
private:
|
2011-05-05 01:59:48 +00:00
|
|
|
typedef SampleView INHERITED;
|
2009-05-05 23:13:23 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
static SkView* MyFactory() { return new LayerMaskView; }
|
|
|
|
static SkViewRegister reg(MyFactory);
|