OLD | NEW |
1 | 1 |
2 /* | 2 /* |
3 * Copyright 2012 Google Inc. | 3 * Copyright 2012 Google Inc. |
4 * | 4 * |
5 * Use of this source code is governed by a BSD-style license that can be | 5 * Use of this source code is governed by a BSD-style license that can be |
6 * found in the LICENSE file. | 6 * found in the LICENSE file. |
7 */ | 7 */ |
8 #include "SampleCode.h" | 8 #include "SampleCode.h" |
9 #include "SkBlurMaskFilter.h" | 9 #include "SkBlurMaskFilter.h" |
10 #include "SkColorPriv.h" | 10 #include "SkColorPriv.h" |
11 #include "SkCanvas.h" | 11 #include "SkCanvas.h" |
12 #include "SkRandom.h" | 12 #include "SkRandom.h" |
13 | 13 |
| 14 SkScalar get_anim_sin(SkMSec time, SkScalar amplitude, SkScalar periodInSec, SkS
calar phaseInSec) { |
| 15 if (!periodInSec) { |
| 16 return 0; |
| 17 } |
| 18 double t = (double)time / 1000.0 + phaseInSec; |
| 19 t *= SkScalarToFloat(2 * SK_ScalarPI) / periodInSec; |
| 20 amplitude = SK_ScalarHalf * amplitude; |
| 21 return amplitude * SkDoubleToScalar(sin(t)) + amplitude; |
| 22 } |
| 23 |
14 class AnimBlurView : public SampleView { | 24 class AnimBlurView : public SampleView { |
15 public: | 25 public: |
16 AnimBlurView() { | 26 AnimBlurView() : fBlurSigma(0), fCircleRadius(100) {} |
17 } | |
18 | 27 |
19 protected: | 28 protected: |
20 // overrides from SkEventSink | 29 // overrides from SkEventSink |
21 virtual bool onQuery(SkEvent* evt) { | 30 bool onQuery(SkEvent* evt) SK_OVERRIDE { |
22 if (SampleCode::TitleQ(*evt)) { | 31 if (SampleCode::TitleQ(*evt)) { |
23 SampleCode::TitleR(evt, "AnimBlur"); | 32 SampleCode::TitleR(evt, "AnimBlur"); |
24 return true; | 33 return true; |
25 } | 34 } |
26 return this->INHERITED::onQuery(evt); | 35 return this->INHERITED::onQuery(evt); |
27 } | 36 } |
28 | 37 |
29 virtual void onDrawContent(SkCanvas* canvas) { | 38 void onDrawContent(SkCanvas* canvas) SK_OVERRIDE { |
30 | |
31 SkScalar blurSigma = SampleCode::GetAnimSinScalar(100 * SK_Scalar1, | |
32 4 * SK_Scalar1, | |
33 5 * SK_Scalar1); | |
34 | |
35 SkScalar circleRadius = 3 * SK_Scalar1 + | |
36 SampleCode::GetAnimSinScalar(150 * SK_Scalar1, | |
37 25 * SK_Scalar1, | |
38 3 * SK_Scalar1); | |
39 | |
40 static const SkBlurStyle gStyles[] = { | 39 static const SkBlurStyle gStyles[] = { |
41 kNormal_SkBlurStyle, | 40 kNormal_SkBlurStyle, |
42 kInner_SkBlurStyle, | 41 kInner_SkBlurStyle, |
43 kSolid_SkBlurStyle, | 42 kSolid_SkBlurStyle, |
44 kOuter_SkBlurStyle, | 43 kOuter_SkBlurStyle, |
45 }; | 44 }; |
46 SkRandom random; | 45 SkRandom random; |
47 | 46 |
48 for (size_t i = 0; i < SK_ARRAY_COUNT(gStyles); ++i) { | 47 for (size_t i = 0; i < SK_ARRAY_COUNT(gStyles); ++i) { |
49 SkMaskFilter* mf = SkBlurMaskFilter::Create( | 48 SkMaskFilter* mf = SkBlurMaskFilter::Create( |
50 gStyles[i], | 49 gStyles[i], |
51 blurSigma, | 50 fBlurSigma, |
52 SkBlurMaskFilter::kHighQuality_BlurFlag); | 51 SkBlurMaskFilter::kHighQuality_BlurFlag); |
53 SkPaint paint; | 52 SkPaint paint; |
54 SkSafeUnref(paint.setMaskFilter(mf)); | 53 SkSafeUnref(paint.setMaskFilter(mf)); |
55 paint.setColor(random.nextU() | 0xff000000); | 54 paint.setColor(random.nextU() | 0xff000000); |
56 canvas->drawCircle(200 * SK_Scalar1 + 400 * (i % 2) * SK_Scalar1, | 55 canvas->drawCircle(200 * SK_Scalar1 + 400 * (i % 2) * SK_Scalar1, |
57 200 * SK_Scalar1 + i / 2 * 400 * SK_Scalar1, | 56 200 * SK_Scalar1 + i / 2 * 400 * SK_Scalar1, |
58 circleRadius, paint); | 57 fCircleRadius, paint); |
59 } | 58 } |
60 this->inval(NULL); | 59 } |
| 60 |
| 61 bool onAnimatePulse(SkMSec curr, SkMSec prev) SK_OVERRIDE { |
| 62 fBlurSigma = get_anim_sin(curr, 100, 4, 5); |
| 63 fCircleRadius = 3 + get_anim_sin(curr, 150, 25, 3); |
| 64 return true; |
61 } | 65 } |
62 | 66 |
63 private: | 67 private: |
64 typedef SkView INHERITED; | 68 SkScalar fBlurSigma, fCircleRadius; |
| 69 |
| 70 typedef SampleView INHERITED; |
65 }; | 71 }; |
66 | 72 |
67 ////////////////////////////////////////////////////////////////////////////// | 73 ////////////////////////////////////////////////////////////////////////////// |
68 | 74 |
69 static SkView* MyFactory() { return new AnimBlurView; } | 75 static SkView* MyFactory() { return new AnimBlurView; } |
70 static SkViewRegister reg(MyFactory); | 76 static SkViewRegister reg(MyFactory); |
OLD | NEW |