blob: e4fa79d47e4ef134997e99389715874831e73089 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
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 */
reed@google.com7a253372011-05-04 14:34:56 +00008#include "SampleCode.h"
9#include "SkView.h"
10#include "SkCanvas.h"
11#include "SkGraphics.h"
12#include "SkRandom.h"
13#include "SkDashPathEffect.h"
14#include "SkShader.h"
15
16static void setBitmapDash(SkPaint* paint, int width) {
17 SkColor c = paint->getColor();
18
19 SkBitmap bm;
commit-bot@chromium.orga8c18312014-02-17 02:55:57 +000020 bm.allocN32Pixels(2, 1);
reed@google.com7a253372011-05-04 14:34:56 +000021 bm.lockPixels();
22 *bm.getAddr32(0, 0) = SkPreMultiplyARGB(0xFF, SkColorGetR(c),
23 SkColorGetG(c), SkColorGetB(c));
24 *bm.getAddr32(1, 0) = 0;
25 bm.unlockPixels();
26
27 SkMatrix matrix;
28 matrix.setScale(SkIntToScalar(width), SK_Scalar1);
29
30 SkShader* s = SkShader::CreateBitmapShader(bm, SkShader::kRepeat_TileMode,
commit-bot@chromium.org9c9005a2014-04-28 14:55:39 +000031 SkShader::kClamp_TileMode, &matrix);
reed@google.com7a253372011-05-04 14:34:56 +000032
33 paint->setShader(s)->unref();
34}
35
36class DashView : public SampleView {
37public:
38 DashView() {
39 this->setBGColor(0xFFDDDDDD);
40 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000041
reed@google.com7a253372011-05-04 14:34:56 +000042protected:
43 // overrides from SkEventSink
44 virtual bool onQuery(SkEvent* evt) {
45 if (SampleCode::TitleQ(*evt)) {
46 SampleCode::TitleR(evt, "Dash");
47 return true;
48 }
49 return this->INHERITED::onQuery(evt);
50 }
51
52 virtual void onDrawContent(SkCanvas* canvas) {
53 static const char* gStr[] = {
54 "11",
55 "44",
56 "112233",
57 "411327463524",
58 };
59
60 SkPaint paint;
61 paint.setStrokeWidth(SkIntToScalar(1));
62
63 SkScalar x0 = SkIntToScalar(10);
64 SkScalar y0 = SkIntToScalar(10);
65 SkScalar x1 = x0 + SkIntToScalar(1000);
66 for (size_t i = 0; i < SK_ARRAY_COUNT(gStr); i++) {
67 SkScalar interval[12];
68 size_t len = SkMin32(strlen(gStr[i]), SK_ARRAY_COUNT(interval));
69 for (size_t j = 0; j < len; j++) {
70 interval[j] = SkIntToScalar(gStr[i][j] - '0');
71 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000072
reed@google.com7a253372011-05-04 14:34:56 +000073 SkDashPathEffect dash(interval, len, 0);
74 paint.setPathEffect(&dash);
75 canvas->drawLine(x0, y0, x1, y0, paint);
76 paint.setPathEffect(NULL);
77
78 y0 += paint.getStrokeWidth() * 3;
79 }
80
81 setBitmapDash(&paint, 3);
82 canvas->drawLine(x0, y0, x1, y0, paint);
83 }
84
85private:
86 typedef SampleView INHERITED;
87};
88
89//////////////////////////////////////////////////////////////////////////////
90
91static SkView* MyFactory() { return new DashView; }
92static SkViewRegister reg(MyFactory);