epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2011 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
reed@google.com | 7a25337 | 2011-05-04 14:34:56 +0000 | [diff] [blame] | 7 | #include "SampleCode.h" |
| 8 | #include "SkView.h" |
| 9 | #include "SkCanvas.h" |
| 10 | #include "SkGraphics.h" |
| 11 | #include "SkRandom.h" |
| 12 | #include "SkDashPathEffect.h" |
| 13 | #include "SkShader.h" |
| 14 | |
| 15 | static void setBitmapDash(SkPaint* paint, int width) { |
| 16 | SkColor c = paint->getColor(); |
| 17 | |
| 18 | SkBitmap bm; |
commit-bot@chromium.org | a8c1831 | 2014-02-17 02:55:57 +0000 | [diff] [blame] | 19 | bm.allocN32Pixels(2, 1); |
reed@google.com | 7a25337 | 2011-05-04 14:34:56 +0000 | [diff] [blame] | 20 | bm.lockPixels(); |
| 21 | *bm.getAddr32(0, 0) = SkPreMultiplyARGB(0xFF, SkColorGetR(c), |
| 22 | SkColorGetG(c), SkColorGetB(c)); |
| 23 | *bm.getAddr32(1, 0) = 0; |
| 24 | bm.unlockPixels(); |
| 25 | |
| 26 | SkMatrix matrix; |
| 27 | matrix.setScale(SkIntToScalar(width), SK_Scalar1); |
| 28 | |
| 29 | SkShader* s = SkShader::CreateBitmapShader(bm, SkShader::kRepeat_TileMode, |
commit-bot@chromium.org | 9c9005a | 2014-04-28 14:55:39 +0000 | [diff] [blame] | 30 | SkShader::kClamp_TileMode, &matrix); |
reed@google.com | 7a25337 | 2011-05-04 14:34:56 +0000 | [diff] [blame] | 31 | |
| 32 | paint->setShader(s)->unref(); |
| 33 | } |
| 34 | |
| 35 | class DashView : public SampleView { |
| 36 | public: |
| 37 | DashView() { |
| 38 | this->setBGColor(0xFFDDDDDD); |
| 39 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 40 | |
reed@google.com | 7a25337 | 2011-05-04 14:34:56 +0000 | [diff] [blame] | 41 | protected: |
| 42 | // overrides from SkEventSink |
| 43 | virtual bool onQuery(SkEvent* evt) { |
| 44 | if (SampleCode::TitleQ(*evt)) { |
| 45 | SampleCode::TitleR(evt, "Dash"); |
| 46 | return true; |
| 47 | } |
| 48 | return this->INHERITED::onQuery(evt); |
| 49 | } |
| 50 | |
| 51 | virtual void onDrawContent(SkCanvas* canvas) { |
| 52 | static const char* gStr[] = { |
| 53 | "11", |
| 54 | "44", |
| 55 | "112233", |
| 56 | "411327463524", |
| 57 | }; |
| 58 | |
| 59 | SkPaint paint; |
| 60 | paint.setStrokeWidth(SkIntToScalar(1)); |
| 61 | |
| 62 | SkScalar x0 = SkIntToScalar(10); |
| 63 | SkScalar y0 = SkIntToScalar(10); |
| 64 | SkScalar x1 = x0 + SkIntToScalar(1000); |
| 65 | for (size_t i = 0; i < SK_ARRAY_COUNT(gStr); i++) { |
| 66 | SkScalar interval[12]; |
| 67 | size_t len = SkMin32(strlen(gStr[i]), SK_ARRAY_COUNT(interval)); |
| 68 | for (size_t j = 0; j < len; j++) { |
| 69 | interval[j] = SkIntToScalar(gStr[i][j] - '0'); |
| 70 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 71 | |
reed@google.com | 7a25337 | 2011-05-04 14:34:56 +0000 | [diff] [blame] | 72 | SkDashPathEffect dash(interval, len, 0); |
| 73 | paint.setPathEffect(&dash); |
| 74 | canvas->drawLine(x0, y0, x1, y0, paint); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 75 | paint.setPathEffect(nullptr); |
reed@google.com | 7a25337 | 2011-05-04 14:34:56 +0000 | [diff] [blame] | 76 | |
| 77 | y0 += paint.getStrokeWidth() * 3; |
| 78 | } |
| 79 | |
| 80 | setBitmapDash(&paint, 3); |
| 81 | canvas->drawLine(x0, y0, x1, y0, paint); |
| 82 | } |
| 83 | |
| 84 | private: |
| 85 | typedef SampleView INHERITED; |
| 86 | }; |
| 87 | |
| 88 | ////////////////////////////////////////////////////////////////////////////// |
| 89 | |
| 90 | static SkView* MyFactory() { return new DashView; } |
| 91 | static SkViewRegister reg(MyFactory); |