epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Copyright 2011 Google Inc. |
| 4 | * |
| 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
| 7 | */ |
yangsu@google.com | 4c295a3 | 2011-06-01 16:11:58 +0000 | [diff] [blame] | 8 | #include "SampleCode.h" |
| 9 | #include "SkCanvas.h" |
| 10 | #include "SkGradientShader.h" |
| 11 | |
| 12 | class GradientView : public SampleView { |
| 13 | public: |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 14 | GradientView() { |
yangsu@google.com | 4c295a3 | 2011-06-01 16:11:58 +0000 | [diff] [blame] | 15 | this->setBGColor(0xFFDDDDDD); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 16 | } |
| 17 | |
yangsu@google.com | 4c295a3 | 2011-06-01 16:11:58 +0000 | [diff] [blame] | 18 | protected: |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 19 | struct GradData { |
yangsu@google.com | 4c295a3 | 2011-06-01 16:11:58 +0000 | [diff] [blame] | 20 | int fCount; |
| 21 | const SkColor* fColors; |
| 22 | const SkScalar* fPos; |
| 23 | }; |
| 24 | // overrides from SkEventSink |
| 25 | virtual bool onQuery(SkEvent* evt) { |
| 26 | if (SampleCode::TitleQ(*evt)) { |
| 27 | SampleCode::TitleR(evt, "Gradients"); |
| 28 | return true; |
| 29 | } |
| 30 | return this->INHERITED::onQuery(evt); |
| 31 | } |
| 32 | virtual void onDrawContent(SkCanvas* canvas) { |
| 33 | SkPaint paint; |
| 34 | paint.setAntiAlias(true); |
| 35 | paint.setStyle(SkPaint::kStroke_Style); |
| 36 | paint.setStrokeWidth(SkScalarHalf(SkIntToScalar(3))); |
| 37 | paint.setStyle(SkPaint::kFill_Style); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 38 | |
yangsu@google.com | 4c295a3 | 2011-06-01 16:11:58 +0000 | [diff] [blame] | 39 | SkPoint p = SkPoint::Make(0,0); |
| 40 | SkPoint q = SkPoint::Make(100,100); |
| 41 | SkPoint pts[] = {p, q}; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 42 | |
yangsu@google.com | 4c295a3 | 2011-06-01 16:11:58 +0000 | [diff] [blame] | 43 | SkScalar t, temp, x, y; |
| 44 | SkColor gColors[] = { |
| 45 | SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorWHITE, SK_ColorBLACK |
| 46 | }; |
| 47 | t = SampleCode::GetAnimScalar(SkIntToScalar(2), SkIntToScalar(20)); |
| 48 | temp = SampleCode::GetAnimScalar(SkIntToScalar(1), SkIntToScalar(8)); |
| 49 | SkScalar step = SK_ScalarPI / (10); |
| 50 | SkScalar angle = t * step; |
| 51 | x = SkScalarSinCos(angle, &y); |
| 52 | SkScalar colorPositions[] = { 0, 0.1 + x, 0.4 + y, 0.9 - x + y, 1.0}; |
| 53 | GradData data = { 5, gColors, colorPositions }; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 54 | |
| 55 | |
yangsu@google.com | 4c295a3 | 2011-06-01 16:11:58 +0000 | [diff] [blame] | 56 | SkRect r = { 0, 0, SkIntToScalar(200), SkIntToScalar(200) }; |
| 57 | SkShader* shader1 = SkGradientShader::CreateLinear( |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 58 | pts, data.fColors, data.fPos,data.fCount, |
yangsu@google.com | 4c295a3 | 2011-06-01 16:11:58 +0000 | [diff] [blame] | 59 | SkShader::kMirror_TileMode); |
| 60 | paint.setShader(shader1)->unref(); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 61 | |
yangsu@google.com | 4c295a3 | 2011-06-01 16:11:58 +0000 | [diff] [blame] | 62 | canvas->drawRect(r, paint); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 63 | |
| 64 | |
yangsu@google.com | 4c295a3 | 2011-06-01 16:11:58 +0000 | [diff] [blame] | 65 | SkPoint s = SkPoint::Make(100,100); |
| 66 | SkShader* shader2 = SkGradientShader::CreateRadial( |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 67 | s, 100, data.fColors, data.fPos, data.fCount, |
yangsu@google.com | 4c295a3 | 2011-06-01 16:11:58 +0000 | [diff] [blame] | 68 | SkShader::kMirror_TileMode); |
| 69 | paint.setShader(shader2)->unref(); |
| 70 | canvas->translate(250, 0); |
| 71 | canvas->drawRect(r, paint); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 72 | |
yangsu@google.com | 4c295a3 | 2011-06-01 16:11:58 +0000 | [diff] [blame] | 73 | SkShader* shader3 = SkGradientShader::CreateTwoPointRadial( |
| 74 | p, 0, q, 100, data.fColors, data.fPos, data.fCount, |
| 75 | SkShader::kMirror_TileMode); |
| 76 | paint.setShader(shader3)->unref(); |
| 77 | canvas->translate(0, 250); |
| 78 | canvas->drawRect(r, paint); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 79 | |
yangsu@google.com | 4c295a3 | 2011-06-01 16:11:58 +0000 | [diff] [blame] | 80 | SkShader* shader4 = SkGradientShader::CreateSweep( |
| 81 | 100, 100, data.fColors, data.fPos, data.fCount); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 82 | |
yangsu@google.com | 4c295a3 | 2011-06-01 16:11:58 +0000 | [diff] [blame] | 83 | paint.setShader(shader4)->unref(); |
| 84 | canvas->translate(-250, 0); |
| 85 | canvas->drawRect(r, paint); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 86 | |
| 87 | this->inval(NULL); |
yangsu@google.com | 4c295a3 | 2011-06-01 16:11:58 +0000 | [diff] [blame] | 88 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 89 | |
yangsu@google.com | 4c295a3 | 2011-06-01 16:11:58 +0000 | [diff] [blame] | 90 | private: |
| 91 | typedef SampleView INHERITED; |
| 92 | }; |
| 93 | |
| 94 | ////////////////////////////////////////////////////////////////////////////// |
| 95 | |
| 96 | static SkView* MyFactory() { return new GradientView; } |
rmistry@google.com | bda03db | 2012-08-14 20:27:54 +0000 | [diff] [blame] | 97 | static SkViewRegister reg(MyFactory); |