1 #include "SampleCode.h" 2 #include "SkView.h" 3 #include "SkCanvas.h" 4 #include "SkImageDecoder.h" 5 #include "SkNinePatch.h" 6 #include "SkPaint.h" 7 #include "SkUnPreMultiply.h" 8 9 class NinePatchView : public SampleView { 10 public: 11 SkBitmap fBM; 12 13 NinePatchView() { 14 SkImageDecoder::DecodeFile("/skimages/btn_default_normal_disable.9.png", &fBM); 15 16 // trim off the edge guide-lines 17 SkBitmap tmp; 18 SkIRect r; 19 r.set(1, 1, fBM.width() - 1, fBM.height() - 1); 20 fBM.extractSubset(&tmp, r); 21 fBM.swap(tmp); 22 23 fX = SkIntToScalar(fBM.width()); 24 fY = 0; 25 } 26 27 protected: 28 // overrides from SkEventSink 29 virtual bool onQuery(SkEvent* evt) { 30 if (SampleCode::TitleQ(*evt)) { 31 SampleCode::TitleR(evt, "NinePatch"); 32 return true; 33 } 34 return this->INHERITED::onQuery(evt); 35 } 36 37 virtual void onDrawBackground(SkCanvas* canvas) { 38 SkPaint p; 39 p.setDither(true); 40 p.setColor(0xFF909090); 41 canvas->drawPaint(p); 42 } 43 44 static void test_rects(SkCanvas* canvas, const SkBitmap& bm, const SkPaint* paint) { 45 static const SkIRect src[] = { 46 { 0, 0, 18, 34 }, 47 { 18, 0, 19, 34 }, 48 { 19, 0, 36, 34 }, 49 { 0, 34, 18, 35 }, 50 { 18, 34, 19, 35 }, 51 { 19, 34, 36, 35 }, 52 { 0, 35, 18, 72 }, 53 { 18, 35, 19, 72 }, 54 { 19, 35, 36, 72 }, 55 }; 56 static const SkRect dst[] = { 57 { 0, 0, 18, 34 }, 58 { 18, 0, 283, 34 }, 59 { 283, 0, 300, 34 }, 60 { 0, 34, 18, 163 }, 61 { 18, 34, 283, 163 }, 62 { 283, 34, 300, 163 }, 63 { 0, 163, 18, 200 }, 64 { 18, 163, 283, 200 }, 65 { 283, 163, 300, 200 }, 66 }; 67 for (size_t i = 0; i < SK_ARRAY_COUNT(src); i++) { 68 canvas->drawBitmapRect(bm, &src[i], dst[i], paint); 69 } 70 } 71 72 virtual void onDrawContent(SkCanvas* canvas) { 73 canvas->drawBitmap(fBM, 0, 0); 74 75 SkIRect margins; 76 SkRect dst; 77 int d = 25; 78 79 margins.set(d, d, d, d); 80 margins.fLeft = fBM.width()/2 - 1; 81 margins.fTop = fBM.height()/2 - 1; 82 margins.fRight = fBM.width() - margins.fLeft - 1; 83 margins.fBottom = fBM.height() - margins.fTop - 1; 84 85 // canvas->translate(fX/5, fY/5); 86 canvas->translate(0, 76); 87 88 dst.set(0, 0, SkIntToScalar(200), SkIntToScalar(200)); 89 90 SkPaint paint; 91 paint.setAntiAlias(false); 92 paint.setDither(true); 93 paint.setFilterBitmap(false); 94 // SkNinePatch::DrawNine(canvas, dst, fBM, margins, &paint); 95 test_rects(canvas, fBM, &paint); 96 } 97 98 virtual SkView::Click* onFindClickHandler(SkScalar x, SkScalar y) { 99 fX = x / 1.5f; 100 fY = y / 1.5f; 101 fX = x; fY = y; 102 this->inval(NULL); 103 return this->INHERITED::onFindClickHandler(x, y); 104 } 105 private: 106 SkScalar fX, fY; 107 typedef SampleView INHERITED; 108 }; 109 110 ////////////////////////////////////////////////////////////////////////////// 111 112 static SkView* MyFactory() { return new NinePatchView; } 113 static SkViewRegister reg(MyFactory); 114 115