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 */ 8 #include "SampleCode.h" 9 #include "SkCanvas.h" 10 #include "SkGradientShader.h" 11 12 class GradientView : public SampleView { 13 public: 14 GradientView() { 15 this->setBGColor(0xFFDDDDDD); 16 } 17 18 protected: 19 struct GradData { 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); 38 39 SkPoint p = SkPoint::Make(0,0); 40 SkPoint q = SkPoint::Make(100,100); 41 SkPoint pts[] = {p, q}; 42 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 }; 54 55 56 SkRect r = { 0, 0, SkIntToScalar(200), SkIntToScalar(200) }; 57 SkShader* shader1 = SkGradientShader::CreateLinear( 58 pts, data.fColors, data.fPos,data.fCount, 59 SkShader::kMirror_TileMode); 60 paint.setShader(shader1)->unref(); 61 62 canvas->drawRect(r, paint); 63 64 65 SkPoint s = SkPoint::Make(100,100); 66 SkShader* shader2 = SkGradientShader::CreateRadial( 67 s, 100, data.fColors, data.fPos, data.fCount, 68 SkShader::kMirror_TileMode); 69 paint.setShader(shader2)->unref(); 70 canvas->translate(250, 0); 71 canvas->drawRect(r, paint); 72 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); 79 80 SkShader* shader4 = SkGradientShader::CreateSweep( 81 100, 100, data.fColors, data.fPos, data.fCount); 82 83 paint.setShader(shader4)->unref(); 84 canvas->translate(-250, 0); 85 canvas->drawRect(r, paint); 86 87 this->inval(NULL); 88 } 89 90 private: 91 typedef SampleView INHERITED; 92 }; 93 94 ////////////////////////////////////////////////////////////////////////////// 95 96 static SkView* MyFactory() { return new GradientView; } 97 static SkViewRegister reg(MyFactory); 98