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 "SkView.h"
10 #include "SkCanvas.h"
11 #include "SkGraphics.h"
12 #include "SkRandom.h"
13 #include "SkDashPathEffect.h"
14 #include "SkShader.h"
15
setBitmapDash(SkPaint * paint,int width)16 static void setBitmapDash(SkPaint* paint, int width) {
17 SkColor c = paint->getColor();
18
19 SkBitmap bm;
20 bm.setConfig(SkBitmap::kARGB_8888_Config, 2, 1);
21 bm.allocPixels();
22 bm.lockPixels();
23 *bm.getAddr32(0, 0) = SkPreMultiplyARGB(0xFF, SkColorGetR(c),
24 SkColorGetG(c), SkColorGetB(c));
25 *bm.getAddr32(1, 0) = 0;
26 bm.unlockPixels();
27
28 SkMatrix matrix;
29 matrix.setScale(SkIntToScalar(width), SK_Scalar1);
30
31 SkShader* s = SkShader::CreateBitmapShader(bm, SkShader::kRepeat_TileMode,
32 SkShader::kClamp_TileMode);
33 s->setLocalMatrix(matrix);
34
35 paint->setShader(s)->unref();
36 }
37
38 class DashView : public SampleView {
39 public:
DashView()40 DashView() {
41 this->setBGColor(0xFFDDDDDD);
42 }
43
44 protected:
45 // overrides from SkEventSink
onQuery(SkEvent * evt)46 virtual bool onQuery(SkEvent* evt) {
47 if (SampleCode::TitleQ(*evt)) {
48 SampleCode::TitleR(evt, "Dash");
49 return true;
50 }
51 return this->INHERITED::onQuery(evt);
52 }
53
onDrawContent(SkCanvas * canvas)54 virtual void onDrawContent(SkCanvas* canvas) {
55 static const char* gStr[] = {
56 "11",
57 "44",
58 "112233",
59 "411327463524",
60 };
61
62 SkPaint paint;
63 paint.setStrokeWidth(SkIntToScalar(1));
64
65 SkScalar x0 = SkIntToScalar(10);
66 SkScalar y0 = SkIntToScalar(10);
67 SkScalar x1 = x0 + SkIntToScalar(1000);
68 for (size_t i = 0; i < SK_ARRAY_COUNT(gStr); i++) {
69 SkScalar interval[12];
70 size_t len = SkMin32(strlen(gStr[i]), SK_ARRAY_COUNT(interval));
71 for (size_t j = 0; j < len; j++) {
72 interval[j] = SkIntToScalar(gStr[i][j] - '0');
73 }
74
75 SkDashPathEffect dash(interval, len, 0);
76 paint.setPathEffect(&dash);
77 canvas->drawLine(x0, y0, x1, y0, paint);
78 paint.setPathEffect(NULL);
79
80 y0 += paint.getStrokeWidth() * 3;
81 }
82
83 setBitmapDash(&paint, 3);
84 canvas->drawLine(x0, y0, x1, y0, paint);
85 }
86
87 private:
88 typedef SampleView INHERITED;
89 };
90
91 //////////////////////////////////////////////////////////////////////////////
92
MyFactory()93 static SkView* MyFactory() { return new DashView; }
94 static SkViewRegister reg(MyFactory);
95
96