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