epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 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 | */ |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 8 | #include "SampleCode.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 9 | #include "SkCanvas.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 10 | #include "SkPaint.h" |
reed@google.com | 40a3772 | 2011-09-06 18:54:54 +0000 | [diff] [blame] | 11 | #include "SkGpuDevice.h" |
| 12 | |
| 13 | static void make_bitmap(SkBitmap* bitmap, GrContext* ctx, SkIRect* center) { |
| 14 | SkDevice* dev; |
| 15 | SkCanvas canvas; |
| 16 | |
| 17 | const int kFixed = 28; |
| 18 | const int kStretchy = 8; |
| 19 | const int kSize = 2*kFixed + kStretchy; |
| 20 | |
| 21 | if (ctx) { |
| 22 | dev = new SkGpuDevice(ctx, SkBitmap::kARGB_8888_Config, kSize, kSize); |
| 23 | *bitmap = dev->accessBitmap(false); |
| 24 | } else { |
| 25 | bitmap->setConfig(SkBitmap::kARGB_8888_Config, kSize, kSize); |
| 26 | bitmap->allocPixels(); |
| 27 | dev = new SkDevice(*bitmap); |
| 28 | } |
| 29 | |
| 30 | canvas.setDevice(dev)->unref(); |
| 31 | canvas.clear(0); |
| 32 | |
| 33 | SkRect r = SkRect::MakeWH(SkIntToScalar(kSize), SkIntToScalar(kSize)); |
| 34 | const SkScalar strokeWidth = SkIntToScalar(6); |
| 35 | const SkScalar radius = SkIntToScalar(kFixed) - strokeWidth/2; |
| 36 | |
| 37 | center->setXYWH(kFixed, kFixed, kStretchy, kStretchy); |
| 38 | |
| 39 | SkPaint paint; |
| 40 | paint.setAntiAlias(true); |
| 41 | |
reed@google.com | 40a3772 | 2011-09-06 18:54:54 +0000 | [diff] [blame] | 42 | paint.setColor(0xFFFF0000); |
| 43 | canvas.drawRoundRect(r, radius, radius, paint); |
| 44 | r.setXYWH(SkIntToScalar(kFixed), 0, SkIntToScalar(kStretchy), SkIntToScalar(kSize)); |
| 45 | paint.setColor(0x8800FF00); |
| 46 | canvas.drawRect(r, paint); |
| 47 | r.setXYWH(0, SkIntToScalar(kFixed), SkIntToScalar(kSize), SkIntToScalar(kStretchy)); |
| 48 | paint.setColor(0x880000FF); |
| 49 | canvas.drawRect(r, paint); |
reed@google.com | 40a3772 | 2011-09-06 18:54:54 +0000 | [diff] [blame] | 50 | } |
| 51 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 52 | |
reed@google.com | 81e3d7f | 2011-06-01 12:42:36 +0000 | [diff] [blame] | 53 | class NinePatchView : public SampleView { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 54 | public: |
reed@google.com | 40a3772 | 2011-09-06 18:54:54 +0000 | [diff] [blame] | 55 | NinePatchView() {} |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 56 | |
| 57 | protected: |
| 58 | // overrides from SkEventSink |
| 59 | virtual bool onQuery(SkEvent* evt) { |
| 60 | if (SampleCode::TitleQ(*evt)) { |
| 61 | SampleCode::TitleR(evt, "NinePatch"); |
| 62 | return true; |
| 63 | } |
| 64 | return this->INHERITED::onQuery(evt); |
| 65 | } |
reed@android.com | 7d970c7 | 2010-04-22 16:07:49 +0000 | [diff] [blame] | 66 | |
reed@google.com | 81e3d7f | 2011-06-01 12:42:36 +0000 | [diff] [blame] | 67 | virtual void onDrawContent(SkCanvas* canvas) { |
reed@google.com | 40a3772 | 2011-09-06 18:54:54 +0000 | [diff] [blame] | 68 | SkBitmap bm; |
| 69 | SkIRect center; |
| 70 | make_bitmap(&bm, SampleCode::GetGr(), ¢er); |
reed@android.com | 7d970c7 | 2010-04-22 16:07:49 +0000 | [diff] [blame] | 71 | |
reed@google.com | 40a3772 | 2011-09-06 18:54:54 +0000 | [diff] [blame] | 72 | // amount of bm that should not be stretched (unless we have to) |
| 73 | const SkScalar fixed = SkIntToScalar(bm.width() - center.width()); |
reed@android.com | 7d970c7 | 2010-04-22 16:07:49 +0000 | [diff] [blame] | 74 | |
reed@google.com | 40a3772 | 2011-09-06 18:54:54 +0000 | [diff] [blame] | 75 | const SkTSize<SkScalar> size[] = { |
| 76 | { fixed * 4 / 5, fixed * 4 / 5 }, // shrink in both axes |
| 77 | { fixed * 4 / 5, fixed * 4 }, // shrink in X |
| 78 | { fixed * 4, fixed * 4 / 5 }, // shrink in Y |
| 79 | { fixed * 4, fixed * 4 } |
| 80 | }; |
| 81 | |
| 82 | canvas->drawBitmap(bm, SkIntToScalar(10), SkIntToScalar(10), NULL); |
| 83 | |
| 84 | SkScalar x = SkIntToScalar(100); |
| 85 | SkScalar y = SkIntToScalar(100); |
| 86 | |
reed@android.com | 7d970c7 | 2010-04-22 16:07:49 +0000 | [diff] [blame] | 87 | SkPaint paint; |
reed@google.com | 40a3772 | 2011-09-06 18:54:54 +0000 | [diff] [blame] | 88 | paint.setFilterBitmap(true); |
| 89 | |
| 90 | for (int iy = 0; iy < 2; ++iy) { |
| 91 | for (int ix = 0; ix < 2; ++ix) { |
| 92 | int i = ix * 2 + iy; |
| 93 | SkRect r = SkRect::MakeXYWH(x + ix * fixed, y + iy * fixed, |
| 94 | size[i].width(), size[i].height()); |
reed@google.com | 9c9b8d9 | 2011-09-07 12:27:43 +0000 | [diff] [blame] | 95 | canvas->drawBitmapNine(bm, center, r, &paint); |
reed@google.com | 40a3772 | 2011-09-06 18:54:54 +0000 | [diff] [blame] | 96 | } |
| 97 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 98 | } |
| 99 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 100 | private: |
reed@android.com | c4cae85 | 2009-09-23 15:06:10 +0000 | [diff] [blame] | 101 | SkScalar fX, fY; |
reed@google.com | 81e3d7f | 2011-06-01 12:42:36 +0000 | [diff] [blame] | 102 | typedef SampleView INHERITED; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 103 | }; |
| 104 | |
| 105 | ////////////////////////////////////////////////////////////////////////////// |
| 106 | |
| 107 | static SkView* MyFactory() { return new NinePatchView; } |
| 108 | static SkViewRegister reg(MyFactory); |
| 109 | |