reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame^] | 1 | #include "SampleCode.h" |
| 2 | #include "SkView.h" |
| 3 | #include "SkCanvas.h" |
| 4 | #include "SkGraphics.h" |
| 5 | #include "SkRandom.h" |
| 6 | #include "SkLayerDrawLooper.h" |
| 7 | #include "SkBlurMaskFilter.h" |
| 8 | |
| 9 | #include <pthread.h> |
| 10 | |
| 11 | #define WIDTH 200 |
| 12 | #define HEIGHT 200 |
| 13 | |
| 14 | class LooperView : public SkView { |
| 15 | public: |
| 16 | |
| 17 | SkLayerDrawLooper* fLooper; |
| 18 | |
| 19 | LooperView() { |
| 20 | static const struct { |
| 21 | SkColor fColor; |
| 22 | SkPaint::Style fStyle; |
| 23 | SkScalar fWidth; |
| 24 | SkScalar fOffset; |
| 25 | int fBlur; |
| 26 | } gParams[] = { |
| 27 | { SK_ColorWHITE, SkPaint::kStroke_Style, SkIntToScalar(1)*3/4, 0, 0 }, |
| 28 | { SK_ColorRED, SkPaint::kStroke_Style, SkIntToScalar(4), 0, 0 }, |
| 29 | { SK_ColorBLUE, SkPaint::kFill_Style, 0, 0, 0 }, |
| 30 | { 0x88000000, SkPaint::kFill_Style, 0, SkIntToScalar(10), 3 } |
| 31 | }; |
| 32 | |
| 33 | fLooper = new SkLayerDrawLooper; |
| 34 | |
| 35 | for (int i = 0; i < SK_ARRAY_COUNT(gParams); i++) { |
| 36 | SkPaint* paint = fLooper->addLayer(gParams[i].fOffset, |
| 37 | gParams[i].fOffset); |
| 38 | paint->setAntiAlias(true); |
| 39 | paint->setColor(gParams[i].fColor); |
| 40 | paint->setStyle(gParams[i].fStyle); |
| 41 | paint->setStrokeWidth(gParams[i].fWidth); |
| 42 | paint->setTextSize(SkIntToScalar(72)); |
| 43 | if (gParams[i].fBlur > 0) { |
| 44 | SkMaskFilter* mf = SkBlurMaskFilter::Create(SkIntToScalar(gParams[i].fBlur), |
| 45 | SkBlurMaskFilter::kNormal_BlurStyle); |
| 46 | paint->setMaskFilter(mf)->unref(); |
| 47 | } |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | virtual ~LooperView() { |
| 52 | fLooper->safeUnref(); |
| 53 | } |
| 54 | |
| 55 | protected: |
| 56 | // overrides from SkEventSink |
| 57 | virtual bool onQuery(SkEvent* evt) { |
| 58 | if (SampleCode::TitleQ(*evt)) { |
| 59 | SampleCode::TitleR(evt, "DrawLooper"); |
| 60 | return true; |
| 61 | } |
| 62 | return this->INHERITED::onQuery(evt); |
| 63 | } |
| 64 | |
| 65 | void drawBG(SkCanvas* canvas) { |
| 66 | canvas->drawColor(0xFFDDDDDD); |
| 67 | // canvas->drawColor(SK_ColorWHITE); |
| 68 | } |
| 69 | |
| 70 | virtual void onDraw(SkCanvas* canvas) { |
| 71 | this->drawBG(canvas); |
| 72 | |
| 73 | SkPaint paint; |
| 74 | paint.setLooper(fLooper); |
| 75 | |
| 76 | canvas->drawCircle(SkIntToScalar(50), SkIntToScalar(50), |
| 77 | SkIntToScalar(30), paint); |
| 78 | |
| 79 | canvas->drawRectCoords(SkIntToScalar(150), SkIntToScalar(50), |
| 80 | SkIntToScalar(200), SkIntToScalar(100), paint); |
| 81 | |
| 82 | canvas->drawText("Looper", 6, SkIntToScalar(230), SkIntToScalar(100), |
| 83 | paint); |
| 84 | } |
| 85 | |
| 86 | virtual SkView::Click* onFindClickHandler(SkScalar x, SkScalar y) { |
| 87 | this->inval(NULL); |
| 88 | return this->INHERITED::onFindClickHandler(x, y); |
| 89 | } |
| 90 | |
| 91 | virtual bool onClick(Click* click) { |
| 92 | return this->INHERITED::onClick(click); |
| 93 | } |
| 94 | |
| 95 | private: |
| 96 | typedef SkView INHERITED; |
| 97 | }; |
| 98 | |
| 99 | ////////////////////////////////////////////////////////////////////////////// |
| 100 | |
| 101 | static SkView* MyFactory() { return new LooperView; } |
| 102 | static SkViewRegister reg(MyFactory); |
| 103 | |